2

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.

PuskarShestha
  • 755
  • 1
  • 7
  • 19

4 Answers4

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
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
0

Use export instead

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

Usman Wali
  • 387
  • 1
  • 3
  • 12
  • What node version do you have ? `$node -v` – Usman Wali Feb 12 '18 at 14:36
  • 8.9.4 . when i checked the process.env in the debugger it does not include the NODE_ENV environment variable. seems like it has not latched to the process object – PuskarShestha Feb 12 '18 at 14:37
  • i tried this program in VS code and wrote the following in powershell. $env:NODE_ENV="development" ; node www . this seems to work. but it is not working in webstorm – PuskarShestha Feb 12 '18 at 15:09
  • If you are using webstrom terminal, according to this: https://stackoverflow.com/questions/7707329/set-node-js-environment-variables-in-webstorm its not possible to directly set node_env using command however you can set `environment variable` in node.js `Run/Debug Configuration` http://www.jetbrains.com/help/webstorm/run-debug-configuration-node-js.html#d149998e16 – Usman Wali Feb 13 '18 at 09:00
0

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).

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
0

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.

jedi_kevo
  • 35
  • 2
  • 8