What this error means

npm ERR! code E401 means npm reached a registry that requires authentication, but the current credentials were missing, expired, or rejected.

Common causes

  • Your npm token expired or was revoked.
  • The project uses a private package scope and the matching registry is not configured.
  • .npmrc contains an old token, wrong registry URL, or malformed auth line.
  • Publishing requires two-factor authentication and the command did not include an OTP.

Quick fixes

  1. Check which registry npm is using:
   npm config get registry
  1. Log in again:
   npm login
  1. If the package uses a scope, verify the scoped registry:
   npm config get @your-scope:registry
  1. Retry the original command.

Step-by-step troubleshooting

  1. Run npm whoami to confirm whether npm sees a valid logged-in user.
  2. Inspect project and user .npmrc files for stale registry or token entries.
  3. Confirm that private package scopes point to the correct registry host.
  4. For publishing, add the OTP when your npm account requires two-factor authentication.
  5. If CI fails, rotate the npm token and update the CI secret value.