Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

node.js - Set up npm credentials over `npm login` without reading input from interactively from stdin

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

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

According to npm-adduser:

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

So how can I run npm login without stdin open?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...