npm ERR! code E401 — Unauthorized Error
Fix 'npm ERR! code E401' errors when installing or publishing private packages. Learn how to authenticate with npm registries.
What Does This Error Mean?
The npm ERR! code E401 means npm could not authenticate with the registry. This happens when trying to install a private package without valid credentials, or when your npm token has expired.
Common Causes
npm token expired or revoked
Not logged into the registry for private packages
Missing or incorrect .npmrc configuration
Using a CI/CD environment without proper npm token setup
Trying to access a private package without access permissions
Registry URL configuration mismatch
How to Fix It
Log in to the registry
Use npm login or configure an authentication token.
# Login interactively npm login # Or use a token npm set //registry.npmjs.org/:_authToken=YOUR_TOKEN # For GitHub Packages npm set //npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKEN
Configure .npmrc
Set up authentication for private registries in .npmrc.
# ~/.npmrc or project .npmrc
registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
# For scoped packages
@mycompany:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}Check CI/CD token configuration
Ensure CI/CD environments have the NPM_TOKEN or similar env variable set.
# GitHub Actions example
- name: Install dependencies
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm ci
# The token is read from .npmrc that references the env variableRelated Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.