I'm trying to automate npm publish
inside a Docker container, but I receive an error when the npm login
command tries to read the username and email:
npm login << EOF username password email EOF
It works in a Bash terminal, but in a container (without stdin
) it shows error:
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.
How can I run npm login
without using stdin
?
npmrc and put it in local or project . npmrc and run the npm publish command from CI. Explicit npm login is not required.
When you login to npm, a file . npmrc is generated and stored in your home directory. This file contains an authentication token.
You may use this command multiple times with the same user account to authorize on a new machine. When authenticating on a new machine, the username, password and email address must all match with your existing record. npm login is an alias to adduser and behaves exactly the same way.
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
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.
npm set //<registry>/:_authToken $TOKEN
Example for Github Package Registry:
npm set //npm.pkg.github.com/:_authToken $GITHUB_TOKEN
This is the simplest solution that I have found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With