cli: Error: Could not login to npm

I’ve seen this a few times now: I run the CLI, it fails. If I run npm whoami then give npx semantic-release-cli setup another time, it works! Not sure what the deal is, as I’m not changing my login status.

First attempt fails:

$ npx semantic-release-cli setup
? What is your npm registry? https://registry.npmjs.org/
? What is your npm username? zeke
ERR! semantic-release Error: Could not login to npm.
ERR! semantic-release     at getNpmToken (/usr/local/lib/node_modules/semantic-release-cli/src/lib/npm.js:51:21)
ERR! semantic-release     at <anonymous>
ERR! semantic-release     at process._tickCallback (internal/process/next_tick.js:188:7)
ERR! semantic-release  { Error: Could not login to npm.
ERR! semantic-release     at getNpmToken (/usr/local/lib/node_modules/semantic-release-cli/src/lib/npm.js:51:21)
ERR! semantic-release     at <anonymous>
ERR! semantic-release     at process._tickCallback (internal/process/next_tick.js:188:7)
ERR! semantic-release   stack: 'Error: Could not login to npm.\n    at getNpmToken (/usr/local/lib/node_modules/semantic-release-cli/src/lib/npm.js:51:21)\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:188:7)' }

But I’m logged in to npm:

$ npm whoami
zeke

Second attempt works:

$ npx semantic-release-cli setup
? What is your npm registry? https://registry.npmjs.org/
? What is your npm username? zeke
? What is your GitHub username? zeke

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 29 (11 by maintainers)

Commits related to this issue

Most upvoted comments

This should be fixed in 3.6.5. Please let us know if the issue is indeed fixed.

I’m new to semantic-release-cli and I sometimes get this issue. First time I ran setup it logged in fine but subsequent attempts kept failing then after a while, it seemed to work again. Here are my logs running the module with npm_config_loglevel=verbose:

login failure

request uri https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding
verb semantic-release request new user, so can't send auth
info semantic-release attempt registry request try #1 at 10:48:28
verb semantic-release request id 4733ce8b8e603591
http semantic-release request PUT https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding
http semantic-release 201 'https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding'
verb semantic-release headers { 'content-type': 'application/json',
verb semantic-release   'content-encoding': 'gzip',
verb semantic-release   'cache-control': 'max-age=300',
verb semantic-release   'transfer-encoding': 'chunked',
verb semantic-release   'accept-ranges': 'bytes',
verb semantic-release   date: 'Wed, 04 Apr 2018 00:48:29 GMT',
verb semantic-release   via: '1.1 varnish',
verb semantic-release   connection: 'keep-alive',
verb semantic-release   'x-served-by': 'cache-syd18921-SYD',
verb semantic-release   'x-cache': 'MISS',
verb semantic-release   'x-cache-hits': '0',
verb semantic-release   'x-timer': 'S1522802908.204149,VS0,VE1702',
verb semantic-release   vary: 'Accept-Encoding, Accept' }
ERR! semantic-release Error: Could not login to npm.

login success

request uri https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding
verb semantic-release request new user, so can't send auth
info semantic-release attempt registry request try #1 at 10:50:30
verb semantic-release request id 393737a7265f2531
http semantic-release request PUT https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding
http semantic-release 201 'https://registry.npmjs.org/-/user/org.couchdb.user:timiscoding'
verb semantic-release headers { 'content-type': 'application/json',
verb semantic-release   'cache-control': 'max-age=300',
verb semantic-release   'content-length': '127',
verb semantic-release   'accept-ranges': 'bytes',
verb semantic-release   date: 'Wed, 04 Apr 2018 00:50:32 GMT',
verb semantic-release   via: '1.1 varnish',
verb semantic-release   connection: 'keep-alive',
verb semantic-release   'x-served-by': 'cache-syd18935-SYD',
verb semantic-release   'x-cache': 'MISS',
verb semantic-release   'x-cache-hits': '0',
verb semantic-release   'x-timer': 'S1522803031.714634,VS0,VE1682',
verb semantic-release   vary: 'Accept-Encoding, Accept' }
info semantic-release Successfully created npm token.

It appears that on the first try, the response is sending back json data but on subsequent tries, it sends back compressed data and so the code @zeke linked is not extracting the token.

Update: Replacing https://github.com/semantic-release/cli/blob/45c00b42defee2f505dbcb1107699e0522c0155b/src/lib/npm.js#L45 with

if (response.headers['content-encoding'] === 'gzip') {
    zlib.gunzip(response.body, (err, buffer) => {
      if (err) reject(err);
      resolve(JSON.parse(buffer.toString('utf8')));
    })
  } else {
    resolve(response.body);
  }
}

seems to have fixed my issue.

could you send a pull request that lower-cases the username?

If you run your command with yarn ..., try running it with npm .... This “solves” the issue for me.

(I know this issue is closed but it comes up in the first results in google so adding this solution seems useful)

The problem come from the npm registry instability. npm-registry-client include a retry mechanism that was alleviating the network/server issues.

It has been changed to handle 409 errors from some “compatible” registries in https://github.com/semantic-release/cli/commit/92e15f717b280bdbee186af7bda45d3749e47b56.

The solution would be to use a proper http client (personally I prefer https://github.com/sindresorhus/got) that would allow to use Promises, setup the authentication (token or user/pass/email) and handle the different errors. And we would need to add a retry mechanism with https://github.com/sindresorhus/p-retry.

Another solution would be to use npm-registry-client again and to promisify it with https://github.com/sindresorhus/pify so we can handle a callback with multiple arguments (err, parsed, raw and response) with multiargs.

I would rather use https://github.com/sindresorhus/got rather than continuing to deal with the multiple issues and shortcomings of npm-registry-client (we had a bunch of problems with it in https://github.com/semantic-release/npm).

From working on this #185 i saw that it always generates token at first, and then complain about not able to login, trying again immediately it should work, since the first try generates the token without you knowing

check https://github.com/theo4u/cli/blob/aac04906701889d4bb17b2ae75671bb2dea012ee/src/lib/npm.js#L46 i resolved it there instead of the body because parsed returns the newly generated token

@gr2m ^^

@simlu sorry that this frustrates you, but please be kind ❤️ we would appreciate if you could delete or edit your comment

Same here. How do you resolve this?

Edit: Nvm, just try again and again… 😃