DEV Community

Cover image for Nodejs axios certificate error
amythical
amythical

Posted on

1

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/

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs