Node.js warns that NODE_TLS_REJECT_UNAUTHORIZED is set to '0'
Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0'Short answer
Node.js prints this warning because the variable NODE_TLS_REJECT_UNAUTHORIZED equals '0', which turns off certificate checks for every TLS connection in the process. Remove the variable, and if a private or corporate CA caused the original certificate error, give Node that CA with NODE_EXTRA_CA_CERTS or the system store.
Fix it fast
1Find out which cause you have
Find out whether the variable is set in the shell you start Node from, and from there trace where it comes from (profile file, .env loader, process manager, or the code itself).
printenv NODE_TLS_REJECT_UNAUTHORIZED grep -rn "NODE_TLS_REJECT_UNAUTHORIZED" . --include="*.js" --include="*.ts" --include="*.json" --include=".env*" --exclude-dir=node_modules2Match what you see, then jump to its fix
- 1
The warning appears on every start, and the variable shows up in printenv, a .env file, a Dockerfile or the codeThe variable was set to silence an earlier certificate errorFix - 2
UNABLE_TO_VERIFY_LEAF_SIGNATURE, DEPTH_ZERO_SELF_SIGNED_CERT or UNABLE_TO_GET_ISSUER_CERT before you disabled the checkThe server uses a private or corporate CA that Node does not knowFix - 3
The same error codes, and the browser or curl on the same machine trusts the siteThe root CA is installed in the operating system store onlyFix
- 1
What does the error look like?
Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0'UNABLE_TO_VERIFY_LEAF_SIGNATUREDEPTH_ZERO_SELF_SIGNED_CERT
Cause 1 of 3
The variable was set to silence an earlier certificate error
1Check
Search your shell profile, deployment config and source for the assignment. Setting it to the value 0 disables certificate validation for TLS connections, and the Node.js CLI documentation strongly discourages that.
grep -rn "NODE_TLS_REJECT_UNAUTHORIZED" ~/.zshrc ~/.bashrc ~/.profile 2>/dev/null
grep -rn "NODE_TLS_REJECT_UNAUTHORIZED" . --exclude-dir=node_modules2Fix
Delete the assignment everywhere it appears, then fix the real certificate problem with one of the two causes below. Leaving the variable at 0 only hides the problem.
unset NODE_TLS_REJECT_UNAUTHORIZED3Verify
Start the app again from a fresh shell; the warning is gone and HTTPS requests still work (or fail with the real certificate error you now fix).
printenv NODE_TLS_REJECT_UNAUTHORIZED
node app.jsConfirmed by nodejs.org
Cause 2 of 3
The server uses a private or corporate CA that Node does not know
1Check
Node uses its own list of well-known root CAs. A certificate signed by an internal CA fails until that CA is added.
node -e "fetch('https://internal.example.com').then(r => console.log(r.status)).catch(e => console.log(e.cause && e.cause.code))"2Fix
Save the CA certificate (and any intermediates) as a PEM file and point NODE_EXTRA_CA_CERTS at it. The documentation says the file extends the well-known roots, and that it is read only when the process starts, so set it before launching Node and not from inside the code.
export NODE_EXTRA_CA_CERTS=/etc/ssl/internal/company-ca.pem
node app.js3Verify
The request that failed now returns a status code and no warning is printed.
NODE_EXTRA_CA_CERTS=/etc/ssl/internal/company-ca.pem node -e "fetch('https://internal.example.com').then(r => console.log(r.status))"Confirmed by nodejs.org
Cause 3 of 3
The root CA is installed in the operating system store only
1Check
Node does not read the operating system trust store unless you ask it to. The TLS documentation suggests --use-system-ca for these error codes when the root CA is installed locally.
node --use-system-ca -e "fetch('https://internal.example.com').then(r => console.log(r.status))"2Fix
Start Node with --use-system-ca, or set NODE_USE_SYSTEM_CA=1 in the environment.
node --use-system-ca app.js
# or
NODE_USE_SYSTEM_CA=1 node app.js3Verify
The connection succeeds without NODE_TLS_REJECT_UNAUTHORIZED in the environment.
printenv NODE_TLS_REJECT_UNAUTHORIZED; node --use-system-ca app.jsConfirmed by nodejs.org
How do I stop it coming back?
- Never ship NODE_TLS_REJECT_UNAUTHORIZED=0 in a Dockerfile, .env file or process manager config; keep CA files in the image instead.
- Pass the CA through NODE_EXTRA_CA_CERTS at process start. If you set the ca option on one request, Node stops using the well-known and extra certificates for that client.
- Fail the CI build when the variable appears in the repository.
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
Node.js says the hostname does not match the certificate's altnames
code: 'ERR_TLS_CERT_ALTNAME_INVALID'
Node.js compares the host you connect to with the subjectAltName entries of the server certificate.
3 causes, each with a fixBack end
Nodemailer fails to send mail: connection timeout, TLS or login errors
ETIMEDOUT with timeoutType connect, greeting, upgrade or socket
Read err.code first: Nodemailer sets it for every failure.
3 causes, each with a fixBack end
Strapi admin blocks uploaded images and media because of its CSP
Refused to load media from 'https://bucket.s3.eu-west-1.amazonaws.com/...' because it violates the following Content Security Policy directive: "media-src 'self' data: blob:".
Strapi's default strapi::security middleware limits images and media to 'self'.
2 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 fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fix