How to fix ‘Error: unable to verify the first certificate’ in Node.js?

Sometimes, we want to fix ‘Error: unable to verify the first certificate’ in Node.js.

In this article, we’ll look at how to fix ‘Error: unable to verify the first certificate’ in Node.js.

How to fix ‘Error: unable to verify the first certificate’ in Node.js?

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.

For instance, we write

require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

to call require with 'ssl-root-cas/latest' to require the root certificate.

Then we can set that has the value of the require('https').globalAgent.options.ca property so https would use the required root certificate.

Conclusion

To fix ‘Error: unable to verify the first certificate’ in Node.js, we should set the appropriate root certificate.