How to fix npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires 2FA in npm
The E401 error indicates an authentication failure when attempting operations on npm's registry. When related to 2FA, it means your npm account has two-factor authentication enabled, but credentials are missing, invalid, or an OTP code is required.
Quick fix
Fixes npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires 2FA
npm login- Check your npm account 2FA settings
- Log in interactively with OTP
- Generate an automation token for CI/CD
- Update .npmrc configuration
npm loginnpm ERR! code E401npm ERR! 401 Unauthorized - This operation requires 2FA
What this error means
The E401 error indicates a general authentication failure when attempting operations on npm's registry (login, publish, etc.). When specifically related to 2FA, it means your npm account has two-factor authentication enabled, but either: - No authentication credentials were provided - The credentials are invalid/expired - A one-time password (OTP) code is missing or incorrect Unlike the EOTP error (which specifically requests an OTP), E401 is a broader authentication rejection that may require different solutions depending on context.
Typical symptoms
- npm ERR! code E401 message appears when running npm login or npm publish
- Authentication fails even with correct password in interactive mode
- CI/CD pipelines fail with authentication errors during automated publish
- Automation scripts hang or timeout waiting for OTP input
- Error message references 'Bearer authorization' or 'Incorrect or missing password'
- Previously working publish workflows suddenly require OTP/2FA
Common causes
- 2FA enabled on account without automation token
- Missing or expired access token in .npmrc
- Account set to 'auth-and-writes' mode requiring OTP on publish
- OTP expired before request completed
- Stale or corrupted npm cache with invalid credentials
- Registry URL mismatch between .npmrc and package-lock.json
- Token lacks 'Bypass 2FA' flag for CI/CD usage
How to fix "npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires 2FA"
Log in to https://npmjs.com, go to Account Settings > Two-Factor Authentication. Note whether 2FA is enabled for 'Authorization Only' or 'Authorization and Writes'. This determines whether you can publish without OTP entry.
For single operations, use npm login on your local machine:
npm loginnpm will prompt for username, password, and then an OTP code from your authenticator app. Have your authenticator ready before starting.
For CI/CD workflows with 2FA enabled:
1. Visit https://npmjs.com/settings/tokens
2. Click 'Generate New Token'
3. Select 'Granular Access Token'
4. Set appropriate permissions (read, publish, or admin)
5. Enable 'Bypass 2FA' for write operations
6. Copy the token and store in your CI/CD provider's secrets manager
export NPM_TOKEN=your_automation_token_hereCreate or update your .npmrc file with your access token:
# Store in ~/.npmrc (user home directory)
//registry.npmjs.org/:_authToken=your_npm_token_hereNever commit .npmrc with tokens to version control. Use environment variables in CI/CD:
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrcIf you must use a password-based workflow with OTP:
# Via environment variable
NPM_CONFIG_OTP=123456 npm publish
# Via command flag
npm publish --otp=123456OTP codes expire within 30-60 seconds, so generate it immediately before running the command.
If auth still fails after trying above steps, clear the npm cache:
npm cache clean --forceThen attempt your operation again. This removes cached credentials that may be invalid.
Advanced notes
E401 vs EOTP Error Distinction: E401 is a general authentication rejection, while EOTP specifically means "this operation requires a one-time password." If you see EOTP, provide OTP via --otp flag. If you see E401, the issue is usually invalid/missing credentials or 2FA misconfiguration.
2FA Modes: npm offers two 2FA modes:
- Authorization Only: Requires OTP only when signing in
- Authorization and Writes: Requires OTP for both login AND publish/package modifications
Token Security (as of Nov 2025): npm deprecated long-lived classic tokens. New write-capable granular tokens default to 7-day expiration (max 90 days). For CI/CD, use trusted publishing (OIDC) or short-lived granular tokens with explicit 2FA bypass.
Frequently asked questions
- What does "npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires" mean?
- ). When specifically related to 2FA, it means your npm account has two-factor authentication enabled, but either: - No authentication credentials were provided - The credentials are invalid/expired - A one-time password (OTP) code is missing or incorrect Unlike the EOTP error (which specifically requests an OTP), E401 is a broader authentication rejection that may require different solutions depending on context.
- How do I fix "npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires"?
- The E401 error indicates an authentication failure when attempting operations on npm's registry. When related to 2FA, it means your npm account has two-factor authentication enabled, but credentials are missing, invalid, or an OTP code is required.
- What causes "npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires"?
- 2FA enabled on account without automation token Missing or expired access token in .npmrc Account set to 'auth-and-writes' mode requiring OTP on publish
- Is the "npm ERR! code E401 npm ERR! 401 Unauthorized - This operation requires" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐Docs: 2FA requirements(Official Docs)
- ๐
- ๐GitHub: npm discussions(GitHub)