shellnpmcommand-lineterminalnpm-login

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:password@github.com

Solution

  • 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 test@example.com
    

    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