Categories
JavaScript Answers

How to fix dotenv file is not loading environment variables with JavaScript?

Spread the love

Sometimes, we want to fix dotenv file is not loading environment variables with JavaScript.

In this article, we’ll look at how to fix dotenv file is not loading environment variables with JavaScript.

How to fix dotenv file is not loading environment variables with JavaScript?

To fix dotenv file is not loading environment variables with JavaScript, we can call config to set the path to the .env file.

For instance, we write

const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, "../.env") });

to call config with an object with the path property that has the full path to the .env file that we want to load.

We call path.resolve with __dirname, which has the path of the project directory and the relative path to the .env file appended to it.

Conclusion

To fix dotenv file is not loading environment variables with JavaScript, we can call config to set the path to the .env file.

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 *