Categories
Vue Answers

How to fix ERR_CERT_AUTHORITY_INVALID error with axios?

Spread the love

The ERR_CERT_AUTHORITY_INVALID error in Axios typically occurs when the SSL certificate presented by the server is not trusted by the client.

This could happen for various reasons, such as using a self-signed certificate or a certificate signed by an unknown or untrusted Certificate Authority (CA).

To fix this error, you have a few options:

1. Install a Trusted SSL Certificate

Obtain and install a trusted SSL certificate from a reputable Certificate Authority (CA) for your server. This is the most reliable and recommended solution, especially for production environments.

2. Bypass SSL Verification (Not Recommended for Production)

If you’re in a development environment or if security is not a concern, you can bypass SSL certificate verification in Axios.

However, this approach is not recommended for production environments because it exposes your application to potential security risks.

const axios = require("axios").default;

// Bypass SSL certificate verification
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

axios
  .get("https://example.com/api")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

In this example, process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' instructs Node.js to bypass SSL certificate verification.

However, remember to remove or comment out this line before deploying your application to production.

3. Add Certificate Authority’s Certificate to Trusted Certificates (Node.js)

If you’re using Node.js, you can add the CA’s certificate to the trusted certificates store.

You can do this by setting the NODE_EXTRA_CA_CERTS environment variable to the path of the CA’s certificate file.

export NODE_EXTRA_CA_CERTS=/path/to/ca-cert.pem

Replace /path/to/ca-cert.pem with the path to the CA’s certificate file.

4. Ensure Correct URL and Certificate Configuration

Double-check the URL you’re requesting and verify that the SSL certificate on the server is correctly configured and valid.

Sometimes, the error may occur due to a misconfiguration on the server side.

Choose the appropriate solution based on your specific circumstances, but always prioritize security considerations, especially in production environments.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *