9

In my node.js application, I have a require('dotenv').config(); line that I need when developing locally in order to use environment variables. When I deploy to AWS however, I need to comment out this line, otherwise the application crashes. Currently I have 4 of these lines and it's a bit annoying to have to keep commenting/uncommenting them when I push/pull the application - is there any workaround for this that removes the need to have to keep removing the line when I deploy to AWS/including the line when I pull and work locally?

user11508332
  • 537
  • 1
  • 11
  • 28

3 Answers3

13

Maybe you can check the value of NODE_ENV (I assume you deploy in production).

Something like:

if (process.env.NODE_ENV === 'development') {
  require('dotenv').config();
}

Or just if NODE_ENV is not production (useful if you have things like NODE_ENV === 'test'):

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • Is NODE_ENV something that's already supposed to be defined in AWS/development? I've used a console.log statement to see that it's undefined in development - I could just use different values for these in prod/development but then wouldn't any variable do the trick? – user11508332 Mar 02 '20 at 00:01
  • Read [NODE_ENV variable in Node.js](https://stackoverflow.com/questions/37728959/node-env-variable-in-node-js) and [Setting NODE_ENV="production"](https://riptutorial.com/node-js/example/10101/setting-node-env--production-) to get an idea about `NODE_ENV`. Do you use any tool/library for development, like nodemon? In production, like AWS, `NODE_ENV` should be `production`. – pzaenger Mar 02 '20 at 00:03
  • but is NODE_ENV necessary, can't I just use any variable that has different values in prod/development? – user11508332 Mar 02 '20 at 00:06
  • It is not necessary, it is more a convenient way. It is also a value of the [user environment](https://nodejs.org/api/process.html#process_process_env). – pzaenger Mar 02 '20 at 00:07
0

Also do take a look at a npm package named config. It gives us multiple files.json with default.json, development.json, local.json. In some cases, we just need to change some keys in development or any other env while we want something the same in all env like port, jwt secret, etc. Take a look at the link

https://www.npmjs.com/package/config

Usama
  • 33
  • 4
  • In that case you can also check [dotenv-defaults](https://www.npmjs.com/package/dotenv-defaults) :) – pzaenger Mar 02 '20 at 10:00
0

IMO it is better to not have code that checks the NODE_ENV and loads things for certain cases.

I put that to a dedicated npm script like npm run dev where I would load the dotenv/config and set the path for the .env file if needed.

In your case the package.json could look like

  "scripts": {
    "dev": "node -r dotenv/config ./src/index.js"
  }

Bonus track: if you have the .env file in the parent folder of your package + enable remote debugger:

  "scripts": {
    "dev": "DOTENV_CONFIG_PATH=../.env node --inspect=0.0.0.0 -r dotenv/config ./src/index.js"
  }

Then you only run npm run dev and it works.

For production, I would then add a different npm script without debugger and dotenv/config loading.

5422m4n
  • 790
  • 5
  • 12