0

I currently have a node.js application where I have a require('dotenv').config(); line that I only want to run when the application is in development, and not production mode.

To achieve this, I want to set a NODE_ENV environment variable to 'development' locally (and set to 'production' when on AWS), and I'll have an set of conditional statements like:

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

I cannot set this NODE_ENV variable in my .env file, as then accessing that would require the require('dotenv').config(); line which will only run depending on whether the if statement evaluates to true. I have tried to set the value of NODE_ENV through set NODE_ENV=development in the terminal in vscode, but it didn't work so I had to do it on command prompt, where it did work (confirmed by typing the command set and seeing NODE_ENV=development), but now when I try to access it through process.env.NODE_ENV, its value is undefined - anyone know the correct procedures to set/access NODE_ENV?

user11508332
  • 537
  • 1
  • 11
  • 28
  • How did you start you node application in the terminal? e.g. `NODE_ENV=developement node your_script.js` – Nico Vignola May 06 '20 at 13:29
  • I use vscode as an IDE and I usually just debug to start the application - I don't run anything in the terminal to start the application – user11508332 May 06 '20 at 13:54
  • do I need to use NODE_ENV in some command? – user11508332 May 06 '20 at 13:58
  • 1
    If you launch your node application trough the debugging section in VS code then you need to setup you env variables in the `launch.json` file. Check the [documentation](https://code.visualstudio.com/docs/editor/variables-reference#_environment-variables). An example on how to set them is also here: https://stackoverflow.com/a/58867939/2362668 – Nico Vignola May 06 '20 at 14:07

0 Answers0