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= Basic realm="", 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.

10

12 Answers

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 
4

Use npm install --registry instead of npm install

4

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

npm logout 

Then

vsts-npm-auth -config .npmrc 
1

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

2

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.

2

Worked for me:

  • Delete the files
  • npm install
1

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

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

1

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= //<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= // // 
1

I solved it running this command:

npm logout/npm login 

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 

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.

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy