Faigjaz Faigjaz - 1 year ago 174
Node.js Question

NodeJS - Get environment variables set using cmd

I just wanted to know if there is any way to simply read environment variables I'vet set using SET in cmd.
I've read about process.env.[ENVVAR], but when I console.log the variable I've set in cmd, it shows undefined.
On other threads I read that it isn't even possible at all to access windows env. variables.
So what is actually right?

Answer Source

I will summarize my comments into an answer.

When you start node.js from a cmd window, a copy of the current user environment is created just for that node.js process. That environment can be accessed via process.env.

That environment will not be changed by any outside agents. Once the node.js process is started, its environment belongs uniquely to the node.js process.

Making changes to the Windows default environment via Windows Control Panel > System > Advanced System Settings > Advanced > Environment Variables affects what variables/values will be set in newly created environments (e.g newly created cmd windows). It does not affect currently open or running environments.

Using process.env, you can read all the existing environment variables in your own environment. You can modify the process.env object directly (changing values, removing properties, etc...) and those changes will be seen by any other code within your process accessing process.env. But outside changes to an environment in some other cmd window will not affect the environment in a running node.js program.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download