DEV Community

Cover image for Nodejs axios certificate error
amythical
amythical

Posted on

Nodejs axios certificate error

TLDR - Solution for axios certificate error if you are using node 8.10.0 or an older node version.

I ran into a certificate error/CERT_HAS_EXPIRED error when running a simple axios get request in nodejs (calling pexels api).
Surprisingly I checked with a CURL call and the request went through fine.

Tip - For debugging api/network calls using curl from the linux instance is very helpful

The Problem
I happened to be using an archaic node version 8.10.0 on that particular software stack.
Apparently some certificate in letsencrypt expired and that has led to this error when calling axios using node 8.10.0 versions.

Here is the link to the stackoverflow thread that helped me solve it - https://stackoverflow.com/questions/69414479/giving-axios-letsencrypts-new-root-certificate-on-old-version-of-node

Diagnosis
I had assumed there was an issue with the endpoint of the api, but the api owners confirmed there wasn't an error.
When I cleaned the npm cache sometimes the error went away.
The curl call from the same instance helped me isolate the issue to something on the instance.

Solution
Create a https agent with the SSL certificate and use it with the axios request.
I found the solution in the stack overflow link.
The link has the code snippet.

Explanation for the error
With the letsencrypt certificate expiring, the openssl library bundled with the older nodejs installation was failing.

Hope this helps someone.

Cover photo credits - Photo by Vie Studio from Pexels: https://www.pexels.com/photo/word-error-on-white-surface-4439425/

Top comments (0)