28

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 "my@email.com\n"
EOD

But without luck.

Note: I will change the strings with env variables

ke_wa
  • 1,176
  • 2
  • 10
  • 12
  • for automation it may be better to set up the ~/.npmrc like this https://stackoverflow.com/a/57014522/442837 – dres Jul 12 '19 at 22:17
  • Does this answer your question? [Set up npm credentials over \`npm login\` without reading input from interactively from stdin](https://stackoverflow.com/questions/23460980/set-up-npm-credentials-over-npm-login-without-reading-input-from-interactively) – Chris Stryczynski Feb 17 '20 at 18:07

9 Answers9

32

@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

Anton Rudeshko
  • 1,039
  • 2
  • 11
  • 20
ke_wa
  • 1,176
  • 2
  • 10
  • 12
11

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
Seralto
  • 1,016
  • 1
  • 16
  • 30
  • maybe this can be done within one line? I have no idea how to put this to gitlab-ci.yml – holms Jun 27 '18 at 11:47
  • @holms, you can create a script with this content and run it via CI. In my case I run it via Dockerfile: `ADD npm-login.sh $APP_HOME/npm-login.sh` `RUN chmod +x $APP_HOME/npm-login.sh` `RUN $APP_HOME/npm-login.sh` – Seralto Jun 27 '18 at 15:26
  • I'm using gitlab-ci so I've ended up with this: https://stackoverflow.com/questions/51062370/how-can-i-add-multi-line-bash-eod-command-to-gitlab-ci-yml/51079382#51079382 – holms Jun 28 '18 at 09:32
5

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.

Pierre Maoui
  • 5,976
  • 2
  • 27
  • 28
4

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
David Boyd
  • 6,501
  • 3
  • 21
  • 13
3

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

2

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.

Aurélien Thieriot
  • 5,853
  • 2
  • 24
  • 25
  • Everyone knows in here that it's stored there, question how to have a single command for generating it. This is required for automation software like ansible and dockerfiles. – holms Jun 27 '18 at 11:34
2

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

Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
2

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
Iain Ballard
  • 4,433
  • 34
  • 39
1

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
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • npm-cli-login depends on `snyk`, snyk downloads its own closed source binary 85MB from `https://static.snyk.io/cli/*/snyk-*`. It is too much for npm auto login, Carl =) I would go with expect instead. – puchu Aug 29 '23 at 08:30