'NPM ERR Code E401: Unable to authenticate, need: Bearer authorization

I downloaded a NodeJS application from GitHub and facing the following error when executing npm install.

npm ERR! code E401
npm ERR! Unable to authenticate, need: Bearer authorization_uri=https://login.windows.net/c1156c2f-a3bb-4fc4-ac07-3eab96da8d10, Basic realm="https://pkgsprodeus21.pkgs.visualstudio.com/", TFS-Federated

My Node version is 6.13.1 and NPM version is 6.13.4. Following is the content of package.json file:

{
  "name": "DemoApp",
  "version": "1.0.0",
  "description": "A social oasis for lovers of pizza.",
  "repository": "****",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "****",
  "license": "MIT",
  "dependencies": {
    "@hapi/boom": "7.4.2",
    "@hapi/catbox": "10.2.1",
    "@hapi/catbox-redis": "5.0.2",
    "@hapi/cookie": "10.1.0",
    "@hapi/good": "8.2.0",
    "@hapi/good-squeeze": "5.2.0",
    "@hapi/hapi": "18.3.1",
    "@hapi/inert": "5.2.1",
    "@hapi/joi": "15.1.0",
    "@hapi/vision": "5.5.2",
    "aws-sdk": "2.488.0",
    "bcryptjs": "2.4.3",
    "bootflat": "2.0.4",
    "fs-extra": "8.1.0",
    "handlebars": "4.1.2",
    "lodash": "4.17.13",
    "pg": "7.11.0",
    "sequelize": "5.9.4"
  }
}

I have been stuck at this issue since yesterday and still no luck finding the solution. Any help would be highly appreciated.



Solution 1:[1]

This is what worked for me.

First, delete the .npmrc file in your Users folder. This folder:

C:\Users\[your user name]

Then run this command in your project folder that has an .npmrc file in it:

npx vsts-npm-auth -config .npmrc

Solution 2:[2]

Use npm install --registry https://registry.npmjs.org instead of npm install

Solution 3:[3]

No need to delete the .npmrc file, the following worked for me

npm logout

Then

vsts-npm-auth -config .npmrc

Solution 4:[4]

If you get E401 with a private npm registry after upgrading to npm v7, remove your package-lock.json and reinstall.

The registry url setting in .npmrc needs to match the http/https protocol in your package-lock.json exactly.

Or as Stuart pointed out: find and replace to update the existing lock file with the correct URL

Solution 5:[5]

I had this exactly same error and turned out it was an issue with personal access token (PAT). Renew your PAT and run vsts-npm-auth.

Solution 6:[6]

Worked for me:

  • Delete the yarn.lock/package-lock.json files
  • npm install

Solution 7:[7]

Delete old .npmrc file from user home directory and then run the following command

vsts-npm-auth -config .npmrc -T $HOME/.npmrc

Solution 8:[8]

This issue comes from a wrong configuration in your .npmrc file. I had a slightly different error so I'm sharing it here.

In my case the exact error was:

Unable to authenticate, need: Bearer realm="<registry>", Basic realm="<registry>"

My npmrc file should connect to a private npm registry and looked like this:

registry=https://<private-registry>
//<private-registry>:_authToken=<token>
//<private-registry>:always-auth=true

The issue was that I needed to add the https:// protocol also to the second and third line and it worked. In the end it looked like this:

registry=https://<private-registry>
    //https://<private-registry>:_authToken=<token>
    //https://<private-registry>:always-auth=true

Solution 9:[9]

I solved it running this command:

npm logout/npm login

Solution 10:[10]

I had the same error with our company registry configured in .npmrc

registry=https:<compnay-registry-url>

Node version : 16.10.0
NPM version : 7.24.0

Solution:

Execute npm login

$ npm login
npm notice Log in on https:<registry-url>
Username: xxxx
Password:
Email: (this IS public) (xxxx)
Logged in as xxx on https:<registry-url>.

After this .npmrc got updated with

//<registry-url>/:_authToken=xxxxx

Solution 11:[11]

I had the same issue, my discovery was as follows:

The application node.js version was 14.0 but the node version in my machine was 16.0. I had to install the node the version 14.0 to resolve the issue.

To manage multiple node versions this tool is highly recommended.

Solution 12:[12]

Had the same error. In my case, the initial project including package-lock.json file was composed with NodeJS version 12 and npm version 6. But trying to run npm install on my local machine with NodeJS version 16 and npm version 7 was causing this problem.

My solution was installing dependencies with the initial version of NodeJS. I used docker to get the correct version, run this command from the root directory of your project (where the package.json file is located):

docker run --rm -it \
    -w /app \
    -v $PWD/:/app \
    node:12 \
    npm install

If you do not have docker installed, you can try using nvm.