I'm trying to automate npm publish
inside a Docker container but I have trouble when the npm login
command tries to read the username and email from prompts:
npm login << EOF
username
password
email
EOF
It works in a Bash terminal but not in a container without stdin open, and shows the following error message:
Username: Password: npm ERR! cb() never called!
npm ERR! not ok code 0
According to npm-adduser:
The username, password, and email are read in from prompts.
So how can I run npm login
without stdin open?
I took a slightly different approach that seems to work great still. To begin with, you will need an auth token. This is easily obtainable by locally running
npm adduser
and then grabbing the generated token from your~/.npmrc
located in your user folder. In order to be authenticated on your ci server this auth token needs to be appended to the registry URL in the user's.npmrc
(similar to how it was locally), not the.npmrc
located in the repo, so these worked great as script steps in my CI configurationwhere AUTH_TOKEN is stored as a secret variable in your settings. A good way to test this is to replace
npm publish
withnpm whoami
to test and make sure it successfully logged you in.Here is my entire publish configuration
I'm using gitlab-ci but I don't see why this wouldn't apply to any ci application.
This builds on top of Alexander F's answer. This is just a simplified version of the code he provided, mashed up with the example code provided by npm-registry-client.
An expect script worked for me. You need to make sure expect is installed, this command should do it for ubuntu:
Your script could look something like this (npm_login_expect):
And then call it like this:
npm-cli-login
allows you to log in to NPM without STDIN.In order to install run:
npm install -g npm-cli-login
Example usage:
Depends on
jq
and three ENV vars set:Based on the answer from https://stackoverflow.com/a/35831310/1663462
This worked in one of my jenkins jobs:
steps
That's all. You could run npm install, and your private modules will be downloaded.