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 badges4 Answers
Reset to default 3You 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
版权声明:本文标题:javascript - NODE_ENV is showing undefined although i set environment variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745419601a2657842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论