Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set npm credentials using `npm login` without reading from stdin?

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?

like image 941
shawnzhu Avatar asked May 04 '14 20:05

shawnzhu


People also ask

Do you have to login to npm?

npmrc and put it in local or project . npmrc and run the npm publish command from CI. Explicit npm login is not required.

Where are npm credentials stored?

When you login to npm, a file . npmrc is generated and stored in your home directory. This file contains an authentication token.

What is npm login used for?

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.


2 Answers

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.

like image 115
danielepolencic Avatar answered Oct 04 '22 18:10

danielepolencic


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.

like image 44
Željko Šević Avatar answered Oct 04 '22 17:10

Željko Šević