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

问题描述 投票:0回答:4

我想知道如何配置

.npmrc
文件,以便我可以拥有默认注册表和具有身份验证的不同范围注册表。

我使用 Nexus 作为私有存储库,我不确定如何为范围注册表设置身份验证,仅设置默认注册表。

例如我的

~/.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=...
部分指定身份验证密钥?

谢谢,

node.js authentication npm nexus npm-publish
4个回答
72
投票

因此,在深入研究 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 编码密码组件。

使用这种方法,只有范围内的软件包将从私有注册表中发布和安装,所有其他软件包将从默认注册表中安装。

编辑:

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

例如:

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=
行。


18
投票

出于某种奇怪的原因,当与作用域包一起使用时,

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

5
投票

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

npm login --scope=@company-scope --registry=https://registry.company-registry.npme.io/

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


-1
投票

如您所知,他们可以将所有 .npmrc-Properties 设置为环境变量,如下所述:https://stackabuse.com/the-ultimate-guide-to-configuring-npm/

如何设置 //nexus:8081/nexus/content/repositories/npm-test/:_authToken=... 作为环境变量?

© www.soinside.com 2019 - 2024. All rights reserved.