javascript - NODE_ENV is showing undefined although i set environment variable - Stack Overflow

I am using webstorm and i tried to set the environment variable usingset NODE_ENV=developmentand when i

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745419602a4626906.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信