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

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.