node.js – Error: unable to verify the first certificate in nodejs

node.js – Error: unable to verify the first certificate in nodejs

Try adding the appropriate root certificate

This is always going to be a much safer option than just blindly accepting unauthorised end points, which should in turn only be used as a last resort.

This can be as simple as adding

require(https) = require(ssl-root-cas/latest).create();

to your application.

The SSL Root CAs npm package (as used here) is a very useful package regarding this problem.

unable to verify the first certificate

The certificate chain is incomplete.

It means that the webserver you are connecting to is misconfigured and did not include the intermediate certificate in the certificate chain it sent to you.

Certificate chain

It most likely looks as follows:

  1. Server certificate – stores a certificate signed by intermediate.
  2. Intermediate certificate – stores a certificate signed by root.
  3. Root certificate – stores a self-signed certificate.

Intermediate certificate should be installed on the server, along with the server certificate.
Root certificates are embedded into the software applications, browsers and operating systems.

The application serving the certificate has to send the complete chain, this means the server certificate itself and all the intermediates. The root certificate is supposed to be known by the client.

Recreate the problem

Go to using your browser.

It doesnt show any error (padlock in the address bar is green).
Its because browsers tend to complete the chain if it’s not sent from the server.

Now, connect to using Node:

// index.js
const axios = require(axios);

  .then(function (response) {
  .catch(function (error) {

Logs: Error: unable to verify the first certificate.


You need to complete the certificate chain yourself.

To do that:

1: You need to get the missing intermediate certificate in .pem format, then

2a: extend Node’s built-in certificate store using NODE_EXTRA_CA_CERTS,

2b: or pass your own certificate bundle (intermediates and root) using ca option.

1. How do I get intermediate certificate?

Using openssl (comes with Git for Windows).

Save the remote servers certificate details:

openssl s_client -connect -servername | tee logcertfile

Were looking for the issuer (the intermediate certificate is the issuer / signer of the server certificate):

openssl x509 -in logcertfile -noout -text | grep -i issuer

It should give you URI of the signing certificate. Download it:

curl --output intermediate.crt

Finally, convert it to .pem:

openssl x509 -inform DER -in intermediate.crt -out intermediate.pem -text


Im using cross-env to set environment variables in package.json file:

start: cross-env NODE_EXTRA_CA_CERTS=C:\Users\USERNAME\Desktop\ssl-connect\intermediate.pem node index.js

2b. ca option

This option is going to overwrite the Nodes built-in root CAs.

Thats why we need to create our own root CA. Use ssl-root-cas.

Then, create a custom https agent configured with our certificate bundle (root and intermediate). Pass this agent to axios when making request.

// index.js
const axios = require(axios);
const path = require(path);
const https = require(https);
const rootCas = require(ssl-root-cas).create();

rootCas.addFile(path.resolve(__dirname, intermediate.pem));
const httpsAgent = new https.Agent({ca: rootCas});

axios.get(, { httpsAgent })
  .then(function (response) {
  .catch(function (error) {

Instead of creating a custom https agent and passing it to axios, you can place the certifcates on the https global agent:

// Applies to ALL requests (whether using https directly or the request module) = rootCas;


  6. How to convert .crt to .pem

node.js – Error: unable to verify the first certificate in nodejs

Another dirty hack, which will make all your requests insecure:


Leave a Reply

Your email address will not be published.