Node.jsHTTPSError Fix2026 Updated

Node.js HTTPS SSL Certificate Error— UNABLE_TO_VERIFY Fixed [2026]

Also covers: mkcert for local HTTPS · rejectUnauthorized · custom CA · axios httpsAgent · expired certs

January 20266 min readNode.js 18/20/22 · TypeScript

⚡ Common SSL Errors

Error
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.

1

Use mkcert for Trusted Local HTTPS (Dev)

Self-signed cert for local development — trusted by OS and Node.js
5 min
mkcert — generate trusted local certificates
# 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.

2

Disable Verification for Dev with rejectUnauthorized: false

Quick local dev fix — never use in production
2 min

Never set rejectUnauthorized: false or NODE_TLS_REJECT_UNAUTHORIZED=0 in production. It disables all TLS verification, making your app vulnerable to MITM attacks.

rejectUnauthorized: false — dev only
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 })
  : undefined
3

Trust a Custom CA Certificate

Corporate proxy or internal CA — add to Node.js trust store
3 min
NODE_EXTRA_CA_CERTS — add custom CA
# 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 } })
})
4

Fix SSL Errors in axios and node-fetch

HTTP client making HTTPS requests to APIs with SSL issues
3 min
axios and node-fetch SSL configuration
import 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
})
5

Fix Expired or Misconfigured Production Certificates

CERT_HAS_EXPIRED or incomplete chain in production
3 min
Diagnose and fix production SSL issues
# 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 nginx

Prevention

Frequently Asked Questions

What does UNABLE_TO_VERIFY_LEAF_SIGNATURE mean in Node.js?+
This error means Node.js cannot verify the SSL certificate chain. The server's certificate is signed by an intermediate CA that Node.js doesn't have in its trust store. This often happens with older certificates or corporate proxies that use certificate inspection. Fix by providing the full certificate chain or adding the CA to NODE_EXTRA_CA_CERTS.
Is it safe to set NODE_TLS_REJECT_UNAUTHORIZED=0?+
No — never in production. Setting NODE_TLS_REJECT_UNAUTHORIZED=0 disables all SSL verification for the entire Node.js process, making every HTTPS request vulnerable to man-in-the-middle attacks. It is acceptable only in local development scripts against known local servers. Always use proper certificates in production.
What is mkcert and how does it solve local HTTPS?+
mkcert is a tool that creates a locally trusted certificate authority (CA) and issues certificates signed by it. Unlike self-signed certificates, mkcert certificates are trusted by your OS and browsers because mkcert installs its CA into the system trust store. This gives you real HTTPS locally without certificate errors.
How do I fix SSL errors in axios with Node.js?+
Pass a custom httpsAgent with rejectUnauthorized: false (dev only) or with a custom CA certificate. Use: const agent = new https.Agent({ rejectUnauthorized: false }); axios.get(url, { httpsAgent: agent }). For production, provide the CA certificate instead of disabling verification.
Why does my Node.js app work in a browser but fail with SSL error?+
Browsers have a larger built-in trust store and often automatically fetch intermediate certificates. Node.js uses OpenSSL with a more limited trust store. If a certificate chain is incomplete (missing intermediate CA), Node.js fails even though browsers succeed. Fix by ensuring your server sends the full certificate chain including intermediates.
How do I add extra CA certificates to Node.js?+
Set the NODE_EXTRA_CA_CERTS environment variable to the path of a PEM file containing your CA certificate(s). Node.js will add these to its trust store in addition to the built-in ones. This is the production-safe way to trust a corporate or custom CA without modifying the system trust store.

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