Node.js fetch fails with unable to get local issuer certificate
unable to get local issuer certificateShort 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.
Fix it fast
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.com2Match what you see, then jump to its fix
- 1
Works 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
Only requests made through one agent or client fail, while others workA custom ca option replaced the default trusted listFix - 3
openssl shows only certificate #0, and other clients may fail tooThe server does not send its intermediate certificateFix
- 1
What does the error look like?
unable to get local issuer certificatecode: '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.js3Verify
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
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.js3Verify
Call one public host and one internal host through the same client.
NODE_EXTRA_CA_CERTS=/etc/ssl/company-ca.pem node server.jsConfirmed by nodejs.org
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 nginxConfirmed by nginx.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Back end
Express rejects a request body as too large
413 (Request Entity Too Large) returned by nginx in front of the app
A 413 means a body was bigger than a size limit.
3 causes, each with a fixBack end
Multer upload fails with 'Unexpected end of form'
Error: Unexpected end of form
Multer reads multipart bodies with busboy, which raises this error when the request body ends before the closing boundary line.
2 causes, each with a fixBack end
Socket.IO answers 404 on the polling request
Cannot GET /socket.io/
A 404 on /socket.io/?EIO=4&transport=polling means the request is not reaching a Socket.IO server.
3 causes, each with a fixBack end
WebSocket handshake fails with HTTP 400 against a Socket.IO server
{"code":1,"message":"Session ID unknown"}
A 400 during the handshake means the server understood the request and rejected it.
3 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fix