Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm install gets E401 from an Azure DevOps Artifacts feed

Errornpm ERR! code E401

Short 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.

  • Applies to Node.js 22, npm 10, Azure Artifacts
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .npmrc
  2. 2Match what you see, then jump to its fix

    1. 1It worked before and now every install of a feed package returns 401.The token in your user-level .npmrc expiredFix
    2. 2There 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. 3The 401 appears only in the pipeline, and the project .npmrc holds only the registry= line.An Azure Pipelines job runs npm without credentialsFix

What does the error look like?

  • npm ERR! code E401
  • npm 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 registry

2Fix

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 .npmrc

3Verify

Install again.

npm install

Confirmed by learn.microsoft.com

Did this fix it?

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" ~/.npmrc

2Fix

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 value

3Verify

The install now fetches a package from the feed.

npm install

Confirmed by learn.microsoft.com

Did this fix it?

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" .npmrc

2Fix

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 ci

3Verify

Re-run the pipeline; the install step passes.

npm ci

Confirmed by learn.microsoft.com

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.