Categories
JavaScript Answers

How to fix Node.js dotenv isn’t loading environment variables?

Spread the love

Sometimes, we want to fix Node.js dotenv isn’t loading environment variables.

In this article, we’ll look at how to fix Node.js dotenv isn’t loading environment variables.

How to fix Node.js dotenv isn’t loading environment variables?

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

For instance, we write

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

to call config with an object with path set to the path of the .env file.

As a result, the .env file’s environment variables will be loaded into process.env.

Conclusion

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

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 *