'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]


Solution 1:[1]

I found a npm package for this some months ago but I forgot to update this question.

Just 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

Solution 2:[2]

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!

Solution 3:[3]

Typing npm login from the command line and entering your credentials will automatically generate an npm token and setup your .npmrc file for you.

Solution 4:[4]

https://stackoverflow.com/a/54540693/6191913 This anwser is working.

To be more helpful, when you type npm login and give username and password interactively, npm will generate an auth_token automatically for you and insert it into the .npmrc.

The auth_token is constant given the username/password is the same.

For cli usage, you can just echo //registry.npmjs.org/:_authToken=npm_MY_TOKEN > ~/.npmrc instead of npm login ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E_net4 - Krabbe mit Hüten
Solution 2 Nathan
Solution 3 SomeRandomPhysicist
Solution 4 silencej