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
@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
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 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
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
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
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.