Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js fetch fails with unable to get local issuer certificate

Errorunable to get local issuer certificate

Short answer

Node cannot link the server's certificate to a CA it trusts. The usual reasons are a private or proxy CA that Node does not know, a custom ca option that replaced the default list, or a server that omits its intermediate certificate. Add the CA with NODE_EXTRA_CA_CERTS or --use-system-ca, or fix the server chain. Do not disable verification.

  • Applies to Node.js 24, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which certificates the server really sends. If only the leaf is listed, the chain is incomplete. If the chain is full and issued by a company or proxy CA, Node needs to trust it.

    openssl s_client -connect api.example.com:443 -servername api.example.com
  2. 2Match what you see, then jump to its fix

    1. 1Works in the browser, fails in Node; common behind a company proxy or with an internal APIThe issuing CA is private or comes from a proxy, so Node does not trust itFix
    2. 2Only requests made through one agent or client fail, while others workA custom ca option replaced the default trusted listFix
    3. 3openssl shows only certificate #0, and other clients may fail tooThe server does not send its intermediate certificateFix

What does the error look like?

  • unable to get local issuer certificate
  • code: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'

Cause 1 of 3

The issuing CA is private or comes from a proxy, so Node does not trust it

1Check

Node uses its own bundled list of well-known CAs and not the operating system store, unless you opt in.

node -e "fetch('https://api.example.com').then(r=>console.log(r.status)).catch(e=>console.log(e.cause?.code))"

2Fix

Point Node at a PEM file with the extra CA. The file is read at process start, so set it before launching. From Node 23.8 (24 LTS has it) you can also trust the operating system store.

NODE_EXTRA_CA_CERTS=/etc/ssl/company-ca.pem node server.js
# or use the operating system's trusted store
node --use-system-ca server.js

3Verify

Run the same request again and expect a status code instead of the error.

NODE_EXTRA_CA_CERTS=/etc/ssl/company-ca.pem node -e "fetch('https://api.example.com').then(r=>console.log(r.status))"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

A custom ca option replaced the default trusted list

1Check

Node's docs say neither the well-known nor the extra certificates are used when the ca option is set explicitly on a TLS or HTTPS client. If your agent lists only your private CA, public sites fail.

grep -rn "ca:" src/

2Fix

Remove the ca option from that client, and add your private CA once for the whole process with NODE_EXTRA_CA_CERTS, which extends the well-known list instead of replacing it.

// before: new https.Agent({ ca: fs.readFileSync('company-ca.pem') })
const agent = new https.Agent();
// start the app with: NODE_EXTRA_CA_CERTS=/etc/ssl/company-ca.pem node server.js

3Verify

Call one public host and one internal host through the same client.

NODE_EXTRA_CA_CERTS=/etc/ssl/company-ca.pem node server.js

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

The server does not send its intermediate certificate

1Check

nginx's docs explain that a CA's chain bundle should be concatenated after the server certificate. Browsers often hide the problem because they already hold common intermediates.

openssl s_client -connect api.example.com:443 -servername api.example.com | grep -c 'BEGIN CERTIFICATE'

2Fix

On the server, use the full chain file as ssl_certificate, with the server certificate first.

ssl_certificate     /etc/ssl/api.example.com.chained.crt;
ssl_certificate_key /etc/ssl/api.example.com.key;

3Verify

Run openssl again and see more than one certificate.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Keep corporate CA files in version control or secrets and pass them with NODE_EXTRA_CA_CERTS in every environment.
  • Never ship NODE_TLS_REJECT_UNAUTHORIZED=0. Node's docs say it makes TLS and HTTPS insecure.
  • Test certificate chains with openssl s_client after every renewal.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.