admin管理员组

文章数量:1426048

I am using webstorm and i tried to set the environment variable using

set NODE_ENV=development

and when i check the for environment variable using

echo%NODE_ENV%

i get development as the answer.

But in my node application when i check for the variable using

 var b= process.env.NODE_ENV;

i get

b:undefined

I even tried using the following in the package.json file

"start": "set  node ./bin/www && NODE_ENV=production "

still i am getting undefined. I dont know whats the problem here.

I am using webstorm and i tried to set the environment variable using

set NODE_ENV=development

and when i check the for environment variable using

echo%NODE_ENV%

i get development as the answer.

But in my node application when i check for the variable using

 var b= process.env.NODE_ENV;

i get

b:undefined

I even tried using the following in the package.json file

"start": "set  node ./bin/www && NODE_ENV=production "

still i am getting undefined. I dont know whats the problem here.

Share Improve this question asked Feb 12, 2018 at 14:12 PuskarShesthaPuskarShestha 8551 gold badge8 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You should first set the variable and then run the script:

"start": "set NODE_ENV=production&& node ./bin/www"

Note that this will only work on Windows. If you want a cross-platform solution, use the cross-env package:

cross-env NODE_ENV=production node ./bin/www

Use export instead

"start": "export NODE_ENV=production && node ./bin/www"

The following is what works for me, as my goal is trying to keep package dependency to what I really need:

"startwin": "SET NODE_ENV=production& SET SOME_TOKEN=123abc& node ./bin/www",
"start": "NODE_ENV=production SOME_TOKEN=123abc node ./bin/www" 

A drawback is each time another environment var is needed, I have 2 places to edit, but that's hardly any effort (at least in my case, yours might be different).

If you have a separate config.js being required, make sure it is first. You may have a race condition.

I ran into something like this yesterday. It may or may not be your issue. When I ran my code, my environment variable was undefined. I moved an entire library into a separate module. I required that module. My problem was that my requires were out of order, so when it asked, it wasn't set yet. I moved my config.js require up above my library package require.

本文标签: javascriptNODEENV is showing undefined although i set environment variableStack Overflow