What to do when npm complains about a self-signed certificate?

Tamás Polgár
Developer rants
Published in
2 min readFeb 15, 2024

Yes, there are other package managers, but it’s not always the developer’s choice. Let’s see another annoying and fairly cryptic problem, and how to get rid of it.

Sometimes when you try to install packages, run a build script, or similar scary things, npm responds in kind, and scares you back with a message like this:

FetchError: request to https://registry.npmjs.org/@moebius%2Fhttp-graceful-shutdown failed, reason: self-signed certificate in certificate chain
at ClientRequest.<anonymous> (C:\Program Files\nodejs\node_modules\npm-check-updates\node_modules\minipass-fetch\lib\index.js:130:14)
at ClientRequest.emit (node:events:514:28)
at TLSSocket.socketErrorListener (node:_http_client:495:9)
at TLSSocket.emit (node:events:526:35)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'SELF_SIGNED_CERT_IN_CHAIN',
errno: 'SELF_SIGNED_CERT_IN_CHAIN',
type: 'system'
}

Kittens may be scared easily, but we’re grown ass cats with claws of wisdom. (Today’s bonmot brought to you by… never mind.) If you see this, do this.

  1. Go to https://registry.npmjs.org
  2. Stare at that JSON for a moment, appreciate its beauty. (Optional.)
  3. Click the icon next to the URL in your browser’s address bar.
This is how it looks like in Chrome. Other browsers may have a lock icon instead, and a different dropdown, but it’s always there. If not, well, use Chrome.

4. Click Connection is secure, then click Certificate is valid.

5. On the panel that appears, go to the Details tab, and click the Export… button on the bottom.

6. Select *.crt as file type. This may be a single certificate or a certificate chain. Both works. Save the exported file locally.

7. Now open a command prompt, and tell npm to use this certificate:

npm config set cafile "C:\certs\whatever.crt"

8. If npm now finally works, throw a 90s themed rave party, and don’t forget to invite me.

--

--