Reputation: 855
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.
Upvotes: 2
Views: 6546
Reputation: 35
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.
Upvotes: 0
Reputation: 10857
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).
Upvotes: 0
Reputation: 417
Use export instead
"start": "export NODE_ENV=production && node ./bin/www"
Upvotes: 0
Reputation: 92521
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
Upvotes: 3