背景
我的
.npmrc
文件似乎已正确读取(从命令行和 Maven 构建中使用 npm config ls -l
检查)。
运行
npm
的机器无法直接连接到网络,它只能连接到 Nexus npm 注册表 URL。因此,未设置代理属性 proxy
和 https-proxy
。
由于对 Nexus 的访问受到限制,我已从 Nexus 内生成了访问令牌。
Nexus 安全令牌由用户名和密码组成,其中都包含
/
等字符,通常必须进行“url 编码”
正如预期的那样,使用此配置,运行时
npm install
检测不到代理。
Nexus npm 注册表代理似乎已正确设置(使用生成的令牌进行连接后,我可以使用网络浏览器访问
json
文件并下载 tgz
文件)
如果我将
registry
设置为 http://registry.npmjs.org/
并注释 _auth
、email
、always-auth
、strict-ssl
属性,并添加 proxy
和 https-proxy
配置,npm install
按预期工作(但是我将无法在目标环境中执行此操作)
.npmrc
文件内容
; Nexus proxy registry pointing to http://registry.npmjs.org/
registry = https://<host>/nexus/content/repositories/npmjs-registry/
; base64 encoded authentication token
_auth = <see question below>
; required by Nexus
email = <valid email>
; force auth to be used for GET requests
always-auth = true
; we don't want to put certificates in .npmrc
strict-ssl = false
loglevel = silly
问题
我应该如何正确生成
_auth
属性才能让 npm install
按预期工作?
到目前为止我已经尝试过了
<username>:<password>
)
npm info retry will retry, error on last attempt: Error: socket hang up
<username>:<password>
))
npm info retry will retry, error on last attempt: Error: This request requires auth credentials. Run `npm login` and repeat the request.
<username>
):
urlencode(<password>
))
npm info retry will retry, error on last attempt: Error: socket hang up
收到
socket hang up
错误时,我有以下堆栈跟踪:
http request GET https://<host>/nexus/content/repositories/npmjs-registry/fsevents
sill fetchPackageMetaData Error: socket hang up
sill fetchPackageMetaData at TLSSocket.onHangUp (_tls_wrap.js:1035:19)
sill fetchPackageMetaData at TLSSocket.g (events.js:260:16)
sill fetchPackageMetaData at emitNone (events.js:72:20)
sill fetchPackageMetaData at TLSSocket.emit (events.js:166:7)
sill fetchPackageMetaData at endReadableNT (_stream_readable.js:905:12)
sill fetchPackageMetaData at doNTCallback2 (node.js:441:9)
sill fetchPackageMetaData at process._tickCallback (node.js:355:17)
sill fetchPackageMetaData error for fsevents@^1.0.0 { [Error: socket hang up] code: 'ECONNRESET' }
WARN install Couldn't install optional dependency: socket hang up
verb install Error: socket hang up
verb install at TLSSocket.onHangUp (_tls_wrap.js:1035:19)
verb install at TLSSocket.g (events.js:260:16)
verb install at emitNone (events.js:72:20)
verb install at TLSSocket.emit (events.js:166:7)
verb install at endReadableNT (_stream_readable.js:905:12)
verb install at doNTCallback2 (node.js:441:9)
verb install at process._tickCallback (node.js:355:17)
收到
This request requires auth credentials
错误时,我有以下堆栈跟踪:
npm sill fetchPackageMetaData Error: This request requires auth credentials. Run `npm login` and repeat the request.
npm sill fetchPackageMetaData at CachingRegistryClient.authify (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\authify.js:17:14)
npm sill fetchPackageMetaData at CachingRegistryClient.makeRequest (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\request.js:103:17)
npm sill fetchPackageMetaData at <root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\request.js:66:17
npm sill fetchPackageMetaData at RetryOperation._fn (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\attempt.js:18:5)
npm sill fetchPackageMetaData at null._onTimeout (<root>\ui\target\node\node_modules\npm\node_modules\retry\lib\retry_operation.js:49:10)
npm sill fetchPackageMetaData at Timer.listOnTimeout (timers.js:92:15)
npm sill fetchPackageMetaData error for fsevents@^1.0.0 [Error: This request requires auth credentials. Run `npm login` and repeat the request.]
npm WARN install Couldn't install optional dependency: This request requires auth credentials. Run `npm login` and repeat the request.
npm verb install Error: This request requires auth credentials. Run `npm login` and repeat the request.
npm verb install at CachingRegistryClient.authify (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\authify.js:17:14)
npm verb install at CachingRegistryClient.makeRequest (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\request.js:103:17)
npm verb install at <root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\request.js:66:17
npm verb install at RetryOperation._fn (<root>\ui\target\node\node_modules\npm\node_modules\npm-registry-client\lib\attempt.js:18:5)
npm verb install at null._onTimeout (<root>\ui\target\node\node_modules\npm\node_modules\retry\lib\retry_operation.js:49:10)
npm verb install at Timer.listOnTimeout (timers.js:92:15)
来源:https://help.sonatype.com/repomanager3/nexus-repository-administration/formats/npm-registry/npm-security & https://help.sonatype.com/repomanager3/nexus-repository-administration/formats/npm-registry/publishing-npm-packages
配置注册表(在步骤 2 中配置身份验证之前执行此操作很重要,因为身份验证设置将基于注册表):
npm config set registry="http://localhost:8081/repository/npm-internal/"
使用如下示例所示的行配置身份验证:
npm config set _auth="$(echo -n 'username:password' | base64)"
使用以下命令检查当前配置:
npm config ls
发布你的 npm 包:
npm publish --registry http://localhost:8081/repository/npm-internal/
如果您有授权令牌,则不应使用用户名:密码。 我建议你:
生成代币
npm config list
npm login --registry=https://nexus.whatever.registry/respository/npm-whatever-group/
//nexus.whatever.registry/respository/npm-whatever-group/:_authToken=NpmToken.YOUR-LOVELY-TOKEN-IN-HEX
//nexus.whatever.registry/respository/npm-whatever-group/:_authToken=NpmToken.YOUR-LOVELY-TOKEN-IN-HEX
email = <EMAIL_USED_FOR_TOKEN_GENERATION>
always-auth = true
registry = https://nexus.whatever.registry/respository/npm-whatever-group/
如果您遇到身份验证/证书问题:
对于 CI/CD 管道(如 gitlabs 或 jenikins):
${RELEVANT_ENV_VARIABLES}
替换 .npmrc 项目文件中的实际值。这样,您将使它们不那么明显,并且始终在管道更改时进行自我更新。希望这有帮助。
查看
registry-client
代码后,我找到了答案,就在这里。我发布它是因为它可能对其他人有帮助:
base64Encode(<username>:<password>)
顺便说一句,有一个 URL 编码,但它是由
authify.js
负责处理的。
我面临的“套接字挂起”问题是由于以下事实:如果在 Windows 配置中设置了代理,则从 CLI(而不是从 Maven 构建)启动
npm
时,所有 ``.npmrc``代理设置似乎被忽略,而本机代理排除(对于公司 URL)被 npm 忽略。我会开一张罚单来报告这种奇怪的行为。
Before you run npm login, please follow the instructions below :
1) Create an ~/.npmrc file with the following contents:
registry=https://example.com/repository/npm-group/
[email protected]
always-auth=true
//example.com/repository/npm-group/:_authToken=
2) run `npm login`
# npm login
Username: firstname.lastname
Password:
Email: (this IS public) [email protected]
Logged in as firstname,lastname on https://example.com/repository/npm-group/.
Use the same password you use to login to example.com
我不了解 Nexus,但我们使用神器作为 npm 存储库,在那里我可以通过调用
base64encode(username:encryptedPassword)
来创建我的身份验证令牌,其中 encryptedPassword
是我从我的个人神器配置文件中获得的令牌。
也许这有帮助。
我的 npmrc 如下:
registry=https://registry.npmjs.org/
@mycompany:registry=https://nexus.mycompany.ninja/repository/npm-hosted/
//nexus.mycompany.ninja/repository/npm-hosted/:_auth="this is my real auth secret="
我刚刚编写了一个包装器,它将凭据存储在系统钥匙串中并动态提供它们。查看:https://github.com/Xiphe/npm_keychain_auth