ke_wa
ke_wa

Reputation: 1246

npm adduser via bash

I want to automate the npm login process via a bash script.

I tried it with this snippet:

/usr/bin/expect -f - <<EOD
spawn npm adduser
expect "Username:"
send "myUserName\n"
expect "mail: (this IS public)"
send "[email protected]\n"
EOD

But without luck.

Note: I will change the strings with env variables

Upvotes: 31

Views: 42422

Answers (9)

uzay95
uzay95

Reputation: 16642

using with npm-cli-login package it worked

# npm install -g npm-cli-login
# npm-cli-login -u myUser -p myPass -e [email protected] -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 [email protected] -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

Upvotes: 1

David Boyd
David Boyd

Reputation: 6601

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

Upvotes: 4

ke_wa
ke_wa

Reputation: 1246

@Aurélien Thieriot: thanks for the hint.

I have two solutions for my problem:

Solution 1

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

Solution 2

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

Upvotes: 34

Pierre Maoui
Pierre Maoui

Reputation: 6384

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.

Upvotes: 7

Salvatore Napoli
Salvatore Napoli

Reputation: 444

My Solution is to use plugin npm-login-cmd

npm install -g npm-login-cmd

export NPM_USER=user
export NPM_PASS=pass
export NPM_EMAIL=valid email syntax
npx npm-login-cmd

login work on enterprice npm repository

Upvotes: 3

Seralto
Seralto

Reputation: 1076

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

Upvotes: 13

Iain Ballard
Iain Ballard

Reputation: 4828

I found that on Windows Server 2012R2, there is some odd behaviour with service accounts. This method worked for me (as part of a Jenkins build, under bash):

cat > ~/.npmrc <<EOL
//my.local.registry:4873/:_authToken="G....................A=="
always_auth=true
registry=http://my.local.registry:4873/
user=aRegisteredUser
EOL

Upvotes: 2

Adam K Dean
Adam K Dean

Reputation: 7475

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

Upvotes: 2

Aur&#233;lien Thieriot
Aur&#233;lien Thieriot

Reputation: 5923

I don't know if it is in any way secured so please do some research before.

But the fact is that npm is storing all those informations into a file. If you look at:

cat ~/.npmrc

It could be interesting enough so you could do the login dance only once.

Upvotes: 2

Related Questions