如何在.npmrc中为作用域注册表设置_auth?

Wil*_*ich 20 authentication nexus node.js npm npm-publish

我想知道如何配置.npmrc文件,以便我可以拥有默认注册表和具有身份验证的不同范围注册表.

我使用Nexus作为私有存储库,我不知道如何为作用域注册表设置身份验证,只有默认注册表.

例如我的~/.npmrc文件是:

registry=https://registry.npmjs.org/
@test-scope:registry=http://nexus:8081/nexus/content/repositories/npm-test/
email=test@user.com
_auth="…"
Run Code Online (Sandbox Code Playgroud)

如果我npm publish为一个作用域的包test-scope,我收到一个身份验证错误.

AFAIK,_auth唯一适用于该registry=...部分.有没有办法为该@test-scope:registry=...部分指定auth密钥?

谢谢,

Wil*_*ich 23

因此,经过一些挖掘NPM源代码后,事实证明有一种方法可以做到这一点.

我的解决方案如下:

registry=https://registry.npmjs.org/
@test-scope:registry=http://nexus:8081/nexus/content/repositories/npm-test/
//nexus:8081/nexus/content/repositories/npm-test/:username=admin
//nexus:8081/nexus/content/repositories/npm-test/:_password=YWRtaW4xMjM=
email=…
Run Code Online (Sandbox Code Playgroud)

说明:

范围@test-scope指定registry=在执行npm publish命令时,应将具有范围的包发布到与缺省值不同的注册表.

开始与两行//nexus:8081/...用于指定的凭据两者的范围的信息库username_password其中_password从先前使用的base64编码密码部件_auth凭据.

使用此方法,将仅从私有注册表发布和安装作用域包,并且将从默认注册表安装所有其他包.

编辑:

除此之外,可以将密码指定为环境变量,以使其不以明文形式存储在文件中.

例如:

registry=https://registry.npmjs.org/
@test-scope:registry=http://nexus:8081/nexus/content/repositories/npm-test/
//nexus:8081/nexus/content/repositories/npm-test/:username=admin
//nexus:8081/nexus/content/repositories/npm-test/:_password=${BASE64_PASSWORD}
email=…
Run Code Online (Sandbox Code Playgroud)

此外,使用Nexus时,email=必须指定行.

  • 在不存储计划测试合格的情况下,以标准方式查看我的答案 (2认同)
  • 最后,当与 Nexus 配合使用时,帮助我克服了 Gitlab CICD 的界限!在我的 `.gitlab-ci.yml` 文件中,我设置了 `npm config set 'nexusUrlHere/repository/npm-group/:_auth' "${NEXUS_TOKEN}"`,然后我将 NEXUS_TOKEN 设置为 Gitlab 中的秘密,然后我们就离开了去 :) (2认同)

c0l*_*0l3 7

出于某种奇怪的原因,在使用范围包时_auth会调用它_authToken.如果您使用此功能,则无需将纯文本密码存储在您的.npmrc

registry=https://registry.npmjs.org/
@test-scope:registry=http://nexus:8081/nexus/content/repositories/npm-test/ 
//nexus:8081/nexus/content/repositories/npm-test/:_authToken=...
email=…
Run Code Online (Sandbox Code Playgroud)


Dio*_*oso 5

运行以下命令,将@company-scope替换为范围,将company-registry替换为您的公司\xe2\x80\x99s npm Enterprise 注册表的名称:

\n
npm login --scope=@company-scope --registry=https://registry.company-registry.npme.io/\n
Run Code Online (Sandbox Code Playgroud)\n

此信息可在 npm文档中找到。

\n