Petr Jelínek
Petr Jelínek

Reputation: 1441

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.

Upvotes: 0

Views: 3195

Answers (2)

Girgetto
Girgetto

Reputation: 1086

You need to add require('dotenv').config() in your application

https://www.npmjs.com/package/dotenv#usage

Upvotes: 1

Isac Larsson
Isac Larsson

Reputation: 121

The first thing that pops to my mind is to restart the server. I've forgot about it numerous of times.

Upvotes: 1

Related Questions