OStack程序员社区-中国程序员成长平台

标题: node.js - 如何在 .npmrc 中为范围注册表设置 _auth? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-3 04:06
标题: node.js - 如何在 .npmrc 中为范围注册表设置 _auth?

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

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

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

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

如果我这样做 npm publish对于范围为 test-scope 的包, 我收到身份验证错误。

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

谢谢,



Best Answer-推荐答案


因此,在对 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指定具有范围的包应发布到与默认值不同的注册表 registry=执行 npm publish 时命令。

//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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4