通过`npm login`设置npm凭证,而无需从stdin交互读取输入 [英] Set up npm credentials over `npm login` without reading input from interactively from stdin

查看:1967
本文介绍了通过`npm login`设置npm凭证,而无需从stdin交互读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Docker容器内的npm publish自动化,但是当npm login命令尝试从提示符下读取用户名和电子邮件时,我遇到了麻烦:

I'm trying to automate npm publish inside a Docker container but I have trouble when the npm login command tries to read the username and email from prompts:

npm login << EOF
username
password
email
EOF

它可以在Bash终端中工作,但不能在没有打开stdin的容器中工作,并显示以下错误消息:

It works in a Bash terminal but not in a container without stdin open, and shows the following error message:

Username: Password: npm ERR! cb() never called!
npm ERR! not ok code 0

根据 npm-adduser :

从提示中读取用户名,密码和电子邮件.

The username, password, and email are read in from prompts.

那么我如何在不打开stdin的情况下运行npm login?

So how can I run npm login without stdin open?

推荐答案

TL; DR:直接向注册表提出HTTP请求:

TL;DR: Make an HTTP request directly to the registry:

TOKEN=$(curl -s \
  -H "Accept: application/json" \
  -H "Content-Type:application/json" \
  -X PUT --data '{"name": "username_here", "password": "password_here"}' \
  http://your_registry/-/user/org.couchdb.user:username_here 2>&1 | grep -Po \
  '(?<="token": ")[^"]*')

npm set registry "http://your_registry"
npm set //your_registry/:_authToken $TOKEN

理性

幕后npm adduser在后台向注册表发出HTTP请求.不必强迫adduser以您想要的方式运行,您可以直接向注册表提出请求,而无需通过cli,然后使用npm set设置auth令牌.

Rationale

Behind the scenes npm adduser makes an HTTP request to the registry. Instead of forcing adduser to behave the way you want, you could make the request directly to the registry without going through the cli and then set the auth token with npm set.

源代码提示您可以使用以下有效负载向http://your_registry/-/user/org.couchdb.user:your-username发出PUT请求

The source code suggests that you could make a PUT request to http://your_registry/-/user/org.couchdb.user:your-username with the following payload

{
  name: username,
  password: password
}

这将在注册表中创建一个新用户.

and that would create a new user in the registry.

非常感谢@shawnzhu,他找到了一种更干净的方法来解决问题.

Many thanks to @shawnzhu for having found a more cleaner approach to solve the problem.

这篇关于通过`npm login`设置npm凭证,而无需从stdin交互读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆