gpt4 book ai didi

node.js - 如何在不读取标准输入的情况下使用 `npm login` 设置 npm 凭据?

转载 作者:IT老高 更新时间:2023-10-28 12:36:06 26 4
gpt4 key购买 nike

我正在尝试在 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 :

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

如何在不使用 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 设置身份验证 token ,而不是强制 adduser 以您想要的方式运行。

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

{
name: username,
password: password
}

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

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

关于node.js - 如何在不读取标准输入的情况下使用 `npm login` 设置 npm 凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com