如何在不读取stdin的情况下使用`npm login`设置NPM凭证? [英] How to set npm credentials using `npm login` without reading from stdin?

查看:62
本文介绍了如何在不读取stdin的情况下使用`npm login`设置NPM凭证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化Docker容器中的npm publish,但在npm login命令尝试读取用户名和电子邮件时收到错误:

npm login << EOF
username
password
email
EOF

它在Bash终端中工作,但在容器中(没有stdin)显示错误:

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

根据npm-adduser

用户名、密码和电子邮件是从提示中读入的。

如何在不使用stdin的情况下运行npm login

推荐答案

tl;dr:直接向注册表发出Http请求:

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请求。您可以不通过CLI直接向注册表发出请求,然后使用npm set设置auth内标识,而不是强制adduser以您想要的方式运行。

The source code suggests您可以使用以下负载向http://your_registry/-/user/org.couchdb.user:your-username发出PUT请求

{
  name: username,
  password: password
}

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

非常感谢@shawnzhu找到了更干净的方法来解决问题。

这篇关于如何在不读取stdin的情况下使用`npm login`设置NPM凭证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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