简体   繁体   English

如何使用 API 密钥将 package 发布到 npm?

[英]How do I publish a package to npm using an API key?

NPM gives the ability to create access tokens with the right to publish packages to the NPM registry, assuming that you've set the package settings to "Require two-factor authentication or automation tokens" in "Publishing access" (and so implicitly it requires 2FA to be enabled, I assume). NPM 能够创建访问令牌,并有权将包发布到 NPM 注册表,假设您已将 package 设置设置为“需要双因素身份验证或自动化访问令牌”我假设要启用 2FA)。

How do I use this token to publish?如何使用此令牌发布? I've checked the output of npm publish -- it asks for an OTP code, either as a prompt or a flag, and before I enabled 2FA it asked me for my password again.我检查了npm publish的 output ——它要求提供 OTP 代码,作为提示或标志,在我启用 2FA 之前,它再次要求我输入密码。 Using a fixed token would be easier than having to pull up my authenticator app.使用固定令牌比启动我的身份验证器应用程序更容易。 I could disable 2FA, but getting the token working is preferable.我可以禁用 2FA,但最好让令牌工作。

For verdaccio or similar you can user this:对于 verdaccio 或类似的,你可以使用这个:

> npm config set registry http://verdaccio/npm/

# then create and update local .npmrc file:
> npm config set_authToken=%YOUR_ACCES_TOKEN%

# and publish pointing to your registry
> npm publish --registry http://verdaccio/npm/

for proget:对于proget:

[~]$ npm config set registry http://proget/npm/private-npm

[~]$ npm config set always-auth true
[~]$ npm config set _auth {ENCODEDAPIKEY}
[~]$ npm config set email {email address}

# then as usual
[~]$npm publish --registry {YOUR_REGISTRY}

instructions are here proget and here verdaccio说明在这里 proget这里 verdaccio

The npm registry has 3 different kinds of token: Read-only, Automation, and Publish. npm 注册表具有 3 种不同类型的令牌:只读、自动化和发布。

It sounds like you created a Publish token.听起来您创建了一个发布令牌。 You need to create an Automation token instead, since this is the only one that bypasses 2FA.您需要创建一个自动化令牌,因为这是唯一绕过 2FA 的令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM