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?
TL;DR: Make an HTTP request directly to the registry:
Rationale
Behind the scenes
npm adduser
makes an HTTP request to the registry. Instead of forcingadduser
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 withnpm 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 payloadand that would create a new user in the registry.
Many thanks to @shawnzhu for having found a more cleaner approach to solve the problem.
Hard to believe that after all this time there is still no solution for npm login. Sure you can grab a token once and use it for all your CI needs, but what about the security implications of a never expiring token? And what if one day admins decide that tokens should expire?
Below is my hacky javascript solution using
npm-registry-client
package. Just pass a json string argument and it will login and write an.npmrc
file into your current dir. To log out usenpm logout
as usual.Example input:
One solution is to fetch the token and update the ~/.npmrc
This prevents issues with @scope package retrieval from npmjs
Example for Github Package Registry:
This is the simplest solution that I have found.
For the solution 2 exposed by ke_wa in this duplicated post has worked.
Mashup:
You could use an expect script instead or write a node script that uses pty.js.