我想知道如何配置 .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/