I want to automate the npm login process via a bash script.
I tried it with this snippet:
/usr/bin/expect -f - <
Didn't have luck with any answers above on OSX.
This did work though:
npm install -g npm-cli-adduser
npm-cli-adduser -u username -p password -e email -r https://repo.com/nexus
For people working with a private registry (typically for CI purpose), reaching directly the Rest API may be a solution :
curl -XPUT -H "Content-type: application/json" -d '{ "name": "your_username", "password": "your_password" }' 'http://localhost:4873/-/user/org.couchdb.user:your_username'
This is what npm adduser
is doing behind the scene.
@Aurélien Thieriot: thanks for the hint.
I have two solutions for my problem:
export NPM_AUTH_TOKEN=myToken
export NPM_EMAIL=myEmail
create/override ~/.npmrc
by following shell script:
echo "_auth = $NPM_AUTH_TOKEN" > ~/.npmrc
echo "email = $NPM_EMAIL" >> ~/.npmrc
export NPM_USERNAME=myUsername
export NPM_PASSWORD=myPassword
export NPM_EMAIL=myEmail
I know the order of the questions. So I can do the following:
npm adduser <<!
$NPM_USERNAME
$NPM_PASSWORD
$NPM_EMAIL
!
Note: solution 2 works only when the user isn't added yet
Otherwise the $NPM_PASSWORD
is not necessary
This way works and with a more elegant expect:
/usr/bin/expect <<EOD
spawn npm adduser
expect {
"Username:" {send "$USERNAME\r"; exp_continue}
"Password:" {send "$PASSWORD\r"; exp_continue}
"Email: (this IS public)" {send "$EMAIL\r"; exp_continue}
}
EOD
I had this issue but the only way of getting round it was to wrap expect into a docker image. You can use it like so:
docker run \
-e NPM_USER=$NPM_USER \
-e NPM_PASS=$NPM_PASS \
-e NPM_EMAIL=$NPM_EMAIL \
bravissimolabs/generate-npm-authtoken \
> ~/.npmrc
https://github.com/bravissimolabs/docker-generate-npm-authtoken
using with npm-cli-login package it worked
# npm install -g npm-cli-login
# npm-cli-login -u myUser -p myPass -e t@ex.com -r http://192.168.56.1:4873
Checking if it is installed or not:
# whereis npm-cli-login
npm-cli-login:
# whereis npm-cli-login | grep '/npm-cli-login' -ic
0
After installation:
# npm install -g npm-cli-login
Check if it is installed:
# whereis npm-cli-login
npm-cli-login: /usr/bin/npm-cli-login
# whereis npm-cli-login | grep '/npm-cli-login' -ic
1
Let's login:
# npm-cli-login -u myUser -p myPass -e t@ex.com -r http://192.168.56.1:4873
info attempt registry request try #1 at 10:13:19 PM
http request PUT http://192.168.56.1:4873/-/user/org.couchdb.user:myUser
http 409 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser
info attempt registry request try #1 at 10:13:19 PM
http request GET http://192.168.56.1:4873/-/user/org.couchdb.user:myUser?write=true
http 200 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser?write=true
info attempt registry request try #1 at 10:13:20 PM
http request PUT http://192.168.56.1:4873/-/user/org.couchdb.user:myUser/-rev/undefined
http 201 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser/-rev/undefined
#
# npm whoami
myUser
#
# npm logout
# npm whoami
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-04-21T22_13_42_373Z-debug.log