首页 > 解决方案 > npm login --always-auth doesn't add the appropriate entry to the config

问题描述

I use npm version 6.13.7 and Artifactory repository manager. The registry URL is defined in .npmrc in my project folder:

root@2892c246217b:/build# cat .npmrc
registry=https://<masked>/artifactory/api/npm/<masked>

I want to use an authentication token created via npm login command:

root@2892c246217b:/build# npm login --always-auth
Username: <masked>
Password:
Email: (this IS public) <masked>
Logged in as <masked> on https://<masked>/artifactory/api/npm/<masked>.

Now an install should work, however it doesn't:

root@2892c246217b:/build# npm install
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="Artifactory Realm"

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-02-04T17_28_52_957Z-debug.log

I check /root/.npmrc/ and see that always-auth entry is missing. So I add it via npm config:

root@2892c246217b:/build# npm config set always-auth true

And fetching works fine:

root@2892c246217b:/build# npm --verbose install
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   '--verbose',
npm verb cli   'install' ]
npm info using npm@6.13.7
npm info using node@v10.18.0
npm verb npm-session fd8f804800679bcc
npm info lifecycle <masked>@2.0.0~preinstall: <masked>@2.0.0
npm timing stage:loadCurrentTree Completed in 10ms
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 0ms
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 4ms
npm http fetch GET 200 https://<masked>/artifactory/api/npm/<masked>/<masked> 109ms
npm http fetch GET 200 https://<masked>/artifactory/api/npm/<masked>/<masked> 149ms
npm http fetch GET 200 https://<masked>/artifactory/api/npm/<masked>/<masked>/-/<masked>.tgz 46ms
npm http fetch GET 200 https://<masked>/artifactory/api/npm/<masked>/<masked> 309ms
npm http fetch GET 200 https://<masked>/artifactory/api/npm/<masked>/<masked> 738ms
...

Is that a bug in npm or am I just doing something wrong?

标签: authenticationnpmnpm-installartifactory

解决方案


使用“npm login”命令时似乎根本不需要“always-auth”,但前提是您使用基本身份验证。

我的 .npmrc 看起来像:(使用 npm login 命令)

registry=http://localhost:8081/artifactory/api/npm/npm-virtual/
//localhost:8081/artifactory/api/npm/npm-virtual/:_authToken=<TOKEN_MASKED>

此外,从 Artifactory 的“设置我”页面,似乎没有必要,只有当您使用基本身份验证时:

在此处输入图像描述


推荐阅读