node.js - 如何为 .npmrc 中的作用域注册表设置 _auth?

标签 node.js authentication npm nexus npm-publish

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

我将 Nexus 用于私有(private)存储库,但我不确定如何为作用域注册表设置身份验证,只有默认注册表。

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

registry=https://registry.npmjs.org/
@test-scope:registry=http://nexus:8081/nexus/content/repositories/npm-test/
email=test@user.com
_auth="…"

如果我对范围为 test-scope 的包执行 npm publish,则会收到身份验证错误。

AFAIK,_auth 仅适用于 registry=... 部分。有没有办法为 @test-scope:registry=... 部分指定身份验证 key ?

谢谢,

最佳答案

所以,在对 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=…

说明:

范围 @test-scope 指定在执行 npm publish< 时,应将具有该范围的包发布到与默认 registry= 不同的注册表 命令。

//nexus:8081/... 开头的两行用于为 username_password< 指定作用域存储库的凭据 其中 _password 是来自先前使用的 _auth 凭据的 base64 编码密码组件。

使用这种方法,只会从私有(private)注册表发布和安装范围内的包,而所有其他包都将从默认注册表安装。

编辑:

除此之外,密码可以指定为环境变量,这样它就不会以明文形式存储在文件中。

例如:

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=…

另外,使用 Nexus 时,必须指定 email= 行。

关于node.js - 如何为 .npmrc 中的作用域注册表设置 _auth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404870/

相关文章:

node.js - RabbitMQ:如何限制消费率

Sharepoint:如何部署自定义身份验证提供程序?

node.js - 运行 `ng serve`后出现 `ng update @angular/cli @angular/core`错误,无法为Angular项目提供服务

node.js - 命令 `npm start` 什么都不做

node.js - 更新字典中的“/”在 firebase 中引发错误

node.js - 将 Node 模块的本地修改部署到 Cloud Functions for Firebase

ASP.NET 成员(member)身份 以用户身份登录

javascript - Eslint 不适用于本地系统

javascript - 更新循环内的变量

JavaScript 用户名认证