Set up npm credentials over `npm login` without reading input from interactively from stdin Set up npm credentials over `npm login` without reading input from interactively from stdin docker docker

Set up npm credentials over `npm login` without reading input from interactively from stdin


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.


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.


An expect script worked for me. You need to make sure expect is installed, this command should do it for ubuntu:

apt-get install expect-dev

Your script could look something like this (npm_login_expect):

#!/usr/bin/expect -f# set our args into variablesset i 0; foreach n $argv {set "p[incr i]" $n}set timeout 60#npm login command, add whatever command-line args are necessaryspawn npm loginmatch_max 100000expect "Username"    send "$p1\r"expect "Password"send "$p2\r" expect "Email"send "$p3\r"expect {   timeout      exit 1   eof}

And then call it like this:

expect -f npm_login_expect myuser mypassword "myuser@domain.com"