Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give credentials to npm login command line

I need to pass the credentials for npm login in a script. Is there a way to give the credentials similar to the git credentials?

git clone https://username:[email protected]
like image 820
AVarf Avatar asked Feb 05 '19 17:02

AVarf


People also ask

How do I find my npm credentials?

Once you have created a user and logged in, you can use npm config ls to ensure that the credentials are stored on your client. You can also check that your user has been added to the registry by going to https://npmjs.com/~username.

Do you have to login to npm?

Explicit npm login is not required.


2 Answers

I found an npm package for this:

Install npm-cli-login and in the terminal/scripts use it as below:

npm-cli-login -u testUser -p testPass -e [email protected]

I found two other ways to pass the credentials without the need to use an external command, but be aware that these commands might not work in environments such as Jenkins.

Commands:

# First way
echo -e 'USERNAME\nPASSWORD\nEMAIL' | npm login -e EMAIL -r REGISTRY

# Second way
npm login -e EMAIL -r REGISTRY << EOF
USERNAME
PASSWORD
EMAIL
EOF
like image 121
AVarf Avatar answered Oct 19 '22 02:10

AVarf


Take a look at the .npmrc file you can use this file to set npm configuration variables, such as credentials, registry location, etc... This file is located in your HOME directory. Here is an example .npmrc file to use for reference:

~/.npmrc

registry=https://registry.npmjs.com/
_auth="<token>"
email=<email>
always-auth=true

substitute your email and _auth token appropriately for your credentials. Your script will use these global configurations set within your .npmrc file.

Hopefully that helps!

like image 19
Nathan Avatar answered Oct 19 '22 00:10

Nathan