npm ERR! code E401 Unable to authenticate — how to fix
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="Artifactory Realm"
npm ERR! code E401
npm ERR! Incorrect or missing password.
Quick fix: Find the stale token with npm config list and cat ~/.npmrc, delete the old _authToken line, then log in again with npm login --registry=<url>. For CI, put //registry.npmjs.org/:_authToken=${NPM_TOKEN} in the project .npmrc and set NPM_TOKEN as a secret.
What it means
HTTP 401 means the registry did not accept the credentials npm sent (or npm sent none). npm reads tokens from .npmrc files: the project one, your user one (~/.npmrc) and the global one. A token there can be expired, revoked, or attached to a different registry URL than the one npm is actually talking to.
See what npm is using:
npm config get registry
npm config list # shows which .npmrc files are loaded
npm whoami # fails with E401 if the token is bad
Cause 1: expired or revoked token in ~/.npmrc
An old _authToken line overrides everything else. Remove it and log in again:
npm logout
npm login
Or edit ~/.npmrc and delete lines like:
//registry.npmjs.org/:_authToken=npm_xxxxxxxx
Public packages on registry.npmjs.org do not need any token, so a broken token is often the only reason a plain npm install fails with E401.
Cause 2: private registry needs its own login
For Artifactory, Nexus, Azure Artifacts or GitHub Packages, log in against that URL:
npm login --registry=https://npm.example.com/
For scoped packages, map the scope to the registry and attach the token to the same host:
@mycompany:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
The //host/path/:_authToken prefix must match the registry URL exactly (including any path such as /api/npm/npm-local/), otherwise npm will not send the token.
GitHub Packages tokens need the read:packages scope.
Cause 3: CI has no token
In CI the user ~/.npmrc from your laptop does not exist. Commit a project .npmrc that reads an environment variable, and store the token as a CI secret:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
# GitHub Actions
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org
- run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Never commit the literal token.
Cause 4: lockfile points at a registry you can't access
package-lock.json stores a resolved URL for every package. If it was generated against a company registry, npm ci will go there even if your registry config is the public one:
grep -m5 '"resolved"' package-lock.json
Either log in to that registry, or regenerate the lockfile against yours:
rm -rf node_modules package-lock.json
npm install
Cause 5: publishing
npm publish with E401 usually means you are not logged in on that machine, or 2FA is required. Run npm whoami; if that fails, npm login first. Automation tokens are the token type meant for publishing from CI.
Last updated 2026-12-01.