npm install gets E401 from an Azure DevOps Artifacts feed
npm ERR! code E401Short answer
npm reached your Azure Artifacts feed but sent no valid credentials. Locally, the token in your user-level .npmrc has expired or was never written: run vsts-npm-auth again, or paste a Base64-encoded PAT. In a pipeline, add the npmAuthenticate task before npm runs.
Fix it fast
1Find out which cause you have
Print the registry npm uses in this folder, then confirm the feed URL in .npmrc matches the one on the Connect to feed page.
npm config get registry cat .npmrc2Match what you see, then jump to its fix
- 1
It worked before and now every install of a feed package returns 401.The token in your user-level .npmrc expiredFix - 2
There is no ;begin auth token block in your user-level .npmrc, or _password holds the raw PAT.macOS, Linux or a manual setup: the credential lines are missing or wrongFix - 3
The 401 appears only in the pipeline, and the project .npmrc holds only the registry= line.An Azure Pipelines job runs npm without credentialsFix
- 1
What does the error look like?
npm ERR! code E401npm ERR! Unable to authenticate, your authentication token seems to be invalid.401 Unauthorized
Cause 1 of 3
The token in your user-level .npmrc expired
1Check
Azure documents that npm returns a 401 Unauthorized error when the token expires, and that you run the sign-in command again.
npm config get registry2Fix
Refresh the token with vsts-npm-auth against the project .npmrc. Azure lists this tool on its Windows tab.
npm install -g vsts-npm-auth --registry https://registry.npmjs.com --always-auth false
vsts-npm-auth -config .npmrc3Verify
Install again.
npm installConfirmed by learn.microsoft.com
Cause 2 of 3
macOS, Linux or a manual setup: the credential lines are missing or wrong
1Check
Credentials go in the user-level .npmrc, not the one in the repository. _password must be the Base64-encoded personal access token, and username can be any non-empty value.
grep -n "pkgs.dev.azure.com" ~/.npmrc2Fix
Create a PAT with the Packaging > Read & write scope, encode it, and paste it into the credential block that the Connect to feed page shows.
echo -n "YOUR_PERSONAL_ACCESS_TOKEN" | base64
# then in ~/.npmrc
//pkgs.dev.azure.com/<ORG>/<PROJECT>/_packaging/<FEED>/npm/registry/:username=anything
//pkgs.dev.azure.com/<ORG>/<PROJECT>/_packaging/<FEED>/npm/registry/:_password=<BASE64_PAT>
//pkgs.dev.azure.com/<ORG>/<PROJECT>/_packaging/<FEED>/npm/registry/:email=npm requires email to be set but doesn't use the value3Verify
The install now fetches a package from the feed.
npm installConfirmed by learn.microsoft.com
Cause 3 of 3
An Azure Pipelines job runs npm without credentials
1Check
A pipeline has no user-level .npmrc. The npmAuthenticate task writes credentials to the .npmrc you name, for the scope of the build.
grep -n "registry" .npmrc2Fix
Run npmAuthenticate before the step that calls npm. The task docs say not to use it when you also use the npm task.
- task: npmAuthenticate@0
inputs:
workingFile: .npmrc
- script: npm ci3Verify
Re-run the pipeline; the install step passes.
npm ciConfirmed by learn.microsoft.com
How do I stop it coming back?
- Never commit a token to the repository; keep it in the user-level .npmrc or a pipeline secret.
- Put a reminder to refresh the token in the project README.
- Use the npmAuthenticate task for every pipeline that installs from the feed.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
Related fixes
Back end
npm install or publish stops with E401 and a password message
npm ERR! Incorrect or missing password.
E401 means the registry did not accept the login npm sent.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix