I am trying to use the dotenv
NPM package and it is not working for me. I have a file config/config.js
with the following content:
Make sure to set cwd in the pm2 config to the correct directory for any calls to dotenv.config().
Example: Your index.js file is in /app/src, your .env file is in /app. Your index.js file has this
dotenv.config({path: "../.env"});
Your pm2 json config should have this: "cwd": "/app/src", "script": "index.js"
You could also use dotenv.config({path: path.join(__dirname, "../.env")}); to avoid the CWD issue. You will still have a problem if you move the .env or the index.js file relative to each other.