如何在同一注册表的每个范围内以不同用户身份进行 npm 登录?

fra*_*ogs 17 javascript node.js npm npm-install .npmrc

我需要为两个不同的范围设置多个 npm authToken,但两个范围都位于同一注册表中。

@scope1:registry=https://registry.npmjs.org/
@scope2:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Run Code Online (Sandbox Code Playgroud)

我尝试将 authToken 添加到每个范围,如下所示:

@scope1:registry=https://registry.npmjs.org/:_authToken=${TOKEN_1}
@scope2:registry=https://registry.npmjs.org/:_authToken=${TOKEN_2}
Run Code Online (Sandbox Code Playgroud)

但是当我405尝试安装软件包时出现错误。

我尝试阅读docs,但找不到为同一注册表上的不同范围指定不同令牌的方法。

fra*_*ogs 11

我联系了 npm 支持并得到了以下回复:

用户无法对单个注册表进行混合和匹配身份验证。我们建议让一个 npm 用户被授予访问所有包的权限,并为该用户使用令牌。


sfs*_*fsr 7

这是一个有点老套的答案——但它确实有效。至少在我使用它的 github 包上是这样。我认为它可以在 npmjs.org 上运行,但我没有任何私有包可供测试。

@scope1:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=TOKEN1

@scope2:registry=https://npm.pkg.github.com:443
//npm.pkg.github.com:443/:_authToken=TOKEN2

@scope3:registry=https://npm.pkg.github.com:0443
//npm.pkg.github.com:0443/:_authToken=TOKEN3
Run Code Online (Sandbox Code Playgroud)

基本上,只需通过指定端口使它们成为不同的注册表,即使它们是相同的端口。