Npm adduser via bash

I want to automate the npm registration process using a bash script.

I tried this 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 no luck.

Note. I will change the lines with env variables

+15
command-line bash npm
source share
7 answers

@ Aurélien Thieriot: thanks for the tip.

I have two solutions for my problem:

Solution 1

 export NPM_AUTH_TOKEN=myToken export NPM_EMAIL=myEmail 

create / override ~/.npmrc using the following shell script:

 echo "_auth = $NPM_AUTH_TOKEN" > ~/.npmrc echo "email = $NPM_EMAIL" >> ~/.npmrc 

Decision 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 only works when the user has not yet been added.
Otherwise, $NPM_PASSWORD not required

+22
source share

This method also works with a more elegant expectation :

 /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 
+6
source share

I found that in Windows Server 2012R2 there is some odd behavior with service accounts. This method worked for me (as part of the 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 
+2
source share

I do not know if this was protected, so please do some research earlier.

But the fact is that npm stores all this information in a file. If you look at:

 cat ~/.npmrc 

It may be interesting enough that you can only dance once.

+1
source share

I had this problem, but the only way around it is to wrap the wait in the docker image. You can use it like this:

 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

+1
source share

My solution is to use the npm-login-cmd plugin

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

enterprise logon NMP repository

+1
source share

For people working with a private registry (usually for CI purposes), direct access to the Rest API can 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 does behind the scenes.

0
source share

All Articles