0

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.

1
  • Did you restart VS Code from the shell in which you set the variable? (If you set an environment variable at the command line, it will only be seen by processes started by that command line). Commented Oct 14, 2019 at 16:12

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

I work, thanks! But, I suppose that this file should be in .gitignore, right? Because, when I want to store there a password, it would be visible, right?
The password that you want to hide could stay in a file called .env and in the .gitignore you can add the env file. npmjs.com/package/dotenv#faq
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.