NodeJs - Can't set env variables in windows

Hi,

I have so far been unable to get windows to accept environment variables. I have tried from inside the IDE, outside in terminal, and via powershell terminal, but no luck so far.

I am moving to using the dotenv library as a fix, but would love to know if this is an outdated problem (as Mosh Node course seems a bit dated) or if it’s just a problem with my machine or setting variables in windows.

Any help appreciated!

you can set env variables in powershell like this:

$env:TEST = ‘test’

and then read:

$env:TEST

that works for me but node not displaying anything when accessing process.env

Hi try to set your environment variables in the console like:

$env:NODE_ENV="development"
$env:NODE_ENV="production"

then access the NODE_ENV attribute of the env object so write code like:

console.log(NODE_ENV: ${process.env.NODE_ENV});

when you run nodemon hopefully that should print the environment variable that you set in the first step

image

image

It kinda works, the strange part is if I run execute the log script directly from the IDE (jetbrains webstorm) I get ‘undefined’, but if I execute the script using node scriptFile.js in the terminal (also inside the IDE) I get the correct output.

very strange, but thank you for your help!

Hi I think its because this process object is only available inside node runtime, ie: when you execute node <filename>

So it turns out there is a config area hidden away in Webstorm to define env variables for development, I will have to see how it plays out for production/deployment.

Thanks for your help!