require('dotenv').config();
console.log(process.env.MY_ENV_VAR);
// when yours .env is in other place then default, you can set path to it
const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }); //use as many '../' as you need
const config = require('dotenv-config')();
console.log(config.test); // localhost
module.exports = config;
This is used to add a specific path to locate the .env file if we use like this
.config() then this is finding .env file in our cwd - current working directory.
require('dotenv').config({ path: '/custom/path/to/.env' })
this module is used to access environment variable in our application