Node.js HTTPS SSL Certificate Error— UNABLE_TO_VERIFY Fixed [2026]
Also covers: mkcert for local HTTPS · rejectUnauthorized · custom CA · axios httpsAgent · expired certs
⚡ Common SSL Errors
Error: unable to verify the first certificate
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
Error: self-signed certificate
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
Error: certificate has expired
code: 'CERT_HAS_EXPIRED'Common Node.js SSL Certificate Errors
Node.js validates SSL certificates using OpenSSL. The three most common errors are: an incomplete certificate chain (UNABLE_TO_VERIFY_LEAF_SIGNATURE), a self-signed certificate not trusted by Node's CA store (DEPTH_ZERO_SELF_SIGNED_CERT), and an expired certificate (CERT_HAS_EXPIRED). The correct fix depends on the context — development vs production.
Use mkcert for Trusted Local HTTPS (Dev)
Self-signed cert for local development — trusted by OS and Node.js# Install mkcert
# macOS:
brew install mkcert
# Windows (Chocolatey):
choco install mkcert
# Linux:
sudo apt install libnss3-tools
curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo mv mkcert-v*-linux-amd64 /usr/local/bin/mkcert
# 1. Install the local CA into your system trust store
mkcert -install
# 2. Generate certificate for localhost
mkcert localhost 127.0.0.1 ::1
# Creates: localhost+2.pem and localhost+2-key.pem
# 3. Use in Node.js HTTPS server:
import https from "https"
import fs from "fs"
import express from "express"
const app = express()
const server = https.createServer({
key: fs.readFileSync("localhost+2-key.pem"),
cert: fs.readFileSync("localhost+2.pem"),
}, app)
server.listen(3000, () => console.log("HTTPS server running on port 3000"))mkcert certificates are trusted by Chrome, Firefox, Safari, and Node.js — no more certificate warnings or SSL errors in local development.
Disable Verification for Dev with rejectUnauthorized: false
Quick local dev fix — never use in productionNever set rejectUnauthorized: false or NODE_TLS_REJECT_UNAUTHORIZED=0 in production. It disables all TLS verification, making your app vulnerable to MITM attacks.
import https from "https"
// ❌ NEVER in production — dev only
const agent = new https.Agent({ rejectUnauthorized: false })
// fetch with custom agent (Node 18+ undici-based fetch doesn't support agent directly)
// Use node-fetch or axios instead:
// axios:
import axios from "axios"
const response = await axios.get("https://localhost:3001/api", {
httpsAgent: new https.Agent({ rejectUnauthorized: false }) // dev only
})
// Environment variable — disables for entire process (less targeted):
// NODE_TLS_REJECT_UNAUTHORIZED=0 node script.js ← dev scripts only, never production
// Better — use per-request agent only for specific calls:
const devAgent = process.env.NODE_ENV === "development"
? new https.Agent({ rejectUnauthorized: false })
: undefinedTrust a Custom CA Certificate
Corporate proxy or internal CA — add to Node.js trust store# Export your corporate CA certificate as PEM
# (from your corporate IT team or openssl)
# Set env variable to point to the CA cert file
NODE_EXTRA_CA_CERTS=/path/to/corporate-ca.pem node server.js
# In package.json:
{
"scripts": {
"dev": "NODE_EXTRA_CA_CERTS=./certs/corporate-ca.pem nodemon src/index.ts"
}
}
# In code — add CA programmatically for specific requests:
import https from "https"
import fs from "fs"
const ca = fs.readFileSync("./certs/corporate-ca.pem")
const agent = new https.Agent({ ca }) // trust this CA only for this agent
const response = await fetch("https://internal-api.company.com/data", {
// Node 22+ fetch supports dispatcher:
// dispatcher: new undici.Agent({ connect: { ca } })
})Fix SSL Errors in axios and node-fetch
HTTP client making HTTPS requests to APIs with SSL issuesimport https from "https"
import axios from "axios"
import fs from "fs"
// --- axios ---
// Dev — disable verification (dev only)
const devClient = axios.create({
httpsAgent: new https.Agent({ rejectUnauthorized: false })
})
// Production — trust specific CA
const prodClient = axios.create({
httpsAgent: new https.Agent({
ca: fs.readFileSync("./certs/chain.pem"), // full certificate chain
rejectUnauthorized: true,
})
})
// --- node-fetch ---
import fetch from "node-fetch"
const response = await fetch("https://api.example.com", {
agent: new https.Agent({
rejectUnauthorized: false // dev only
})
})
// --- Native Node.js https module ---
import https from "https"
https.get({
hostname: "api.example.com",
path: "/data",
rejectUnauthorized: false, // dev only
}, (res) => {
// handle response
})Fix Expired or Misconfigured Production Certificates
CERT_HAS_EXPIRED or incomplete chain in production# Check certificate expiry and chain:
openssl s_client -connect api.example.com:443 -servername api.example.com
# Check certificate details:
openssl s_client -connect api.example.com:443 2>/dev/null | openssl x509 -noout -dates
# notBefore: ...
# notAfter: ... ← check this date
# Check full chain:
openssl s_client -connect api.example.com:443 -showcerts 2>/dev/null | grep -E "BEGIN|END"
# If UNABLE_TO_VERIFY_LEAF_SIGNATURE — intermediate CA missing from server chain
# Fix: configure your web server to send the full chain (cert + intermediates)
# Nginx — include full chain:
# ssl_certificate /etc/ssl/fullchain.pem; ← full chain (cert + intermediates)
# ssl_certificate_key /etc/ssl/privkey.pem;
# Node.js HTTPS server — include full chain:
https.createServer({
key: fs.readFileSync("privkey.pem"),
cert: fs.readFileSync("fullchain.pem"), // ← fullchain not just cert.pem
}, app)
# Auto-renew with certbot:
# certbot renew --quiet
# Add to cron: 0 0 1 * * certbot renew --quiet && systemctl reload nginxPrevention
- Use mkcert for local HTTPS development — trusted by OS, browsers, and Node.js with no certificate warnings
- Never use rejectUnauthorized: false or NODE_TLS_REJECT_UNAUTHORIZED=0 in production code
- Use NODE_EXTRA_CA_CERTS for corporate or internal CAs — add the CA cert without disabling verification
- In production, always use fullchain.pem (not just cert.pem) — includes intermediate CA certificates
- Set up auto-renewal with certbot and a cron job — CERT_HAS_EXPIRED is always avoidable
- When testing HTTPS in CI, use mkcert or generate a self-signed cert with NODE_EXTRA_CA_CERTS pointing to it
Frequently Asked Questions
What does UNABLE_TO_VERIFY_LEAF_SIGNATURE mean in Node.js?+−
Is it safe to set NODE_TLS_REJECT_UNAUTHORIZED=0?+−
What is mkcert and how does it solve local HTTPS?+−
How do I fix SSL errors in axios with Node.js?+−
Why does my Node.js app work in a browser but fail with SSL error?+−
How do I add extra CA certificates to Node.js?+−
Need Expert Help?
We Build Secure Node.js APIs
Softplix engineers configure Node.js HTTPS, TLS certificates, and secure API architecture for production deployments. Let us help.
Talk to an Engineer