简体   繁体   English

NPM ERR代码E401:无法认证,需要:承载授权

[英]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.我从 GitHub 下载了一个 NodeJS 应用程序,并在执行 npm 安装时遇到以下错误。

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.我的节点版本是 6.13.1,NPM 版本是 6.13.4。 Following is the content of package.json file:以下是 package.json 文件的内容:

{
  "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.任何帮助将不胜感激。

This is what worked for me.这对我有用。

First, delete the .npmrc file in your Users folder.首先,删除用户文件夹中的 .npmrc 文件。 This folder:这个文件夹:

C:\Users\[your user name]

Then run this command in your project folder that has an .npmrc file in it:然后在包含 .npmrc 文件的项目文件夹中运行此命令:

npx vsts-npm-auth -config .npmrc

使用npm install --registry https://registry.npmjs.org而不是npm install

No need to delete the .npmrc file, the following worked for me无需删除 .npmrc 文件,以下对我有用

npm logout

Then然后

vsts-npm-auth -config .npmrc

I had this exactly same error and turned out it was an issue with personal access token (PAT).我遇到了完全相同的错误,结果发现这是个人访问令牌 (PAT) 的问题。 Renew your PAT and run vsts-npm-auth .更新您的 PAT 并运行vsts-npm-auth

If you get E401 with a private npm registry after upgrading to npm v7, remove your package-lock.json and reinstall.如果您在升级到 npm v7 后获得带有私有 npm 注册表的 E401,请删除您的 package-lock.json 并重新安装。

The registry url setting in .npmrc needs to match the http/https protocol in your package-lock.json exactly. .npmrc 中的注册表 url 设置需要与 package-lock.json 中的 http/https 协议完全匹配。

我解决了它运行这个命令:

npm logout/npm login

Delete old .npmrc file from user home directory and then run the following command从用户主目录中删除旧的 .npmrc 文件,然后运行以下命令

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

I had the same error with our company registry configured in .npmrc我在.npmrc中配置的公司注册表也有同样的错误

registry=https:<compnay-registry-url>

Node version: 16.10.0节点版本:16.10.0
NPM version: 7.24.0 NPM 版本:7.24.0

Solution:解决方案:

Execute npm login执行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在此.npmrc更新后

//<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 .应用程序 node.js 版本是14.0但我机器中的节点版本是16.0 I had to install the node the version 14.0 to resolve the issue.我必须安装节点 14.0 版本才能解决问题。

To manage multiple node versions this tool is highly recommended.要管理多个节点版本,强烈建议使用此工具。

I had the same issue caused by an expired token in the .npmrc file in my project directory.我的项目目录中的.npmrc文件中的过期令牌导致了同样的问题。 Sadly the error is very unspecific on what causes the auth error.遗憾的是,该错误对于导致身份验证错误的原因非常不明确。

I downloaded a NodeJS application from GitHub and faces following error when executing npm install.我从 GitHub 下载了一个 NodeJS 应用程序,在执行 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:我的 Node 版本是 6.13.1,NPM 版本是 6.13.4 以下是 package.json 文件的内容:

{
  "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.任何帮助将不胜感激。

Worked for me:为我工作:

  • Delete the yarn.lock/package-lock.json files删除 yarn.lock/package-lock.json 文件
  • npm install安装

This issue comes from a wrong configuration in your .npmrc file.此问题来自 .npmrc 文件中的错误配置。 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:我的 npmrc 文件应该连接到一个私有的 npm 注册表,看起来像这样:

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.问题是我还需要将 https:// 协议添加到第二行和第三行并且它起作用了。 In the end it looked like this:最后它看起来像这样:

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

This worked for me.这对我有用。 I just logged in again in NPM and problem was solved我刚刚在 NPM 再次登录,问题就解决了

npm login

After u write username and password execute again写完用户名和密码后再次执行

mvn install

For me the the problem was in.npmrc file the key password needed to be changed to _password then npm install worked.对我来说,问题出在 .npmrc 文件中,密钥password需要更改为_password然后npm install工作。

Using a Mac and Azure - it was because my Personal Access Token (PAT) had expired and it didn't let me know.使用Mac和 Azure - 这是因为我的个人访问令牌 (PAT) 已过期并且没有通知我。

Open your.npmrc with your auth tokens in.使用您的身份验证令牌打开 your.npmrc。

Go to your azure repo and click your profile and then personal access tokens. Go 到您的 azure 存储库并单击您的个人资料,然后单击个人访问令牌。

Create a new token.创建一个新令牌。

Open terminal and run:打开终端并运行:

node -e "require('readline').createInterface({input:process.stdin,output:process.stdout,historySize:0}).question('PAT> ',p => { b64=Buffer.from(p.trim()).toString('base64');console.log(b64);process.exit(); })"

Paste your PAT in and press Enter粘贴您的 PAT 并按 Enter

Copy the Base64 encoded value into your.npmrc password.将 Base64 编码值复制到 your.npmrc 密码中。

使用命令better-vsts-npm-auth它应该可以工作。

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.在我的例子中,包含package-lock.json文件的初始项目是由package-lock.json版本 12 和 npm 版本 6 组成的。但是尝试在我的本地机器上使用 NodeJS 版本 16 和 npm 版本 7 运行npm install导致了这个问题。

I still do not know what is causing this, but installing dependencies with initial version of NodeJS solved this problem.我仍然不知道是什么导致了这个问题,但是使用 NodeJS 的初始版本安装依赖项解决了这个问题。 Run this command from root directory of your project, where package.json file located:从项目的根目录运行此命令, package.json文件位于:

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

If you are trying to install any package from your private repo and you are getting this error with npm i package_name command then如果您尝试从您的私人仓库安装任何 package 并且您使用npm i package_name命令收到此错误,那么

1. remove your package-lock.json from the the directory where to tried to create the package from 
2. reinstall dependencies: npm i 

this will resolve the issue.这将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM