How do I setup the dotenv file in Node.js?

前端 未结 30 1144

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:



        
30条回答
  •  北海茫月
    2020-12-07 14:59

    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.

提交回复
热议问题