'Environment variable in Node.js (VS Code) is undefined
I am trying to set a environment variable in Windows 10 command line with this command:
set NODE_ENV=production
When I use echo %NODE_ENV%, it is working just fine.
But when I use process.env.NODE_ENV inside VS Code, it is still undefined.
I tried to set this variable inside VS Code command line, but doesn´t work either. This doesn´t work with any environment variable (PORT, etc.)
I also tried to use app.get("env") which is set to development by default. When I set NODE_ENV to something else, it doesn´t work as well.
Do you have any idea, where is the problem? Do I have to configure something somewhere? Thanks for your advice.
Solution 1:[1]
You need to add require('dotenv').config() in your application
Solution 2:[2]
The first thing that pops to my mind is to restart the server. I've forgot about it numerous of times.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Girgetto |
| Solution 2 | Isac Larsson |
