3

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?

8
  • Are you trying to get access to environment variables that were set in the Windows cmd environment BEFORE you ran node.js in that particular cmd shell window? Or environment variables that were set using cmd from within node.js? Please expand on what exactly you're trying to do. Remember that there are many different environments. We need to know which one you're talking about reading and which one the variable you want was set in. Commented Jul 7, 2016 at 6:23
  • I used SET ENVNAME=VALUE in Windows cmd (and I set a system environment variable in Windows). It holds a path to my database and now I want to read it within my application (typescript). I know very little about it, so I'm not sure if this answers your questions. Commented Jul 7, 2016 at 6:29
  • Each cmd window is a separate environment. So, setting an environment variable in one does not affect a different cmd window. But, you can set an environment variable in a cmd window and THEN in that window, start your node.js app and it will then be able to read that environment variable. Commented Jul 7, 2016 at 6:36
  • What about environment variables set in Windows Control Panel > System > Advanced System Settings > Advanced > Environment Variables? Am I able to access those whenever I like to? EDIT: I setting it in the same cmd window as I start my app, but it still shows undefined. :/ Commented Jul 7, 2016 at 6:41
  • Windows Control Panel > System > Advanced System Settings > Advanced > Environment Variables will affect any command windows created AFTER you make the change. Existing cmd environments or programs will not see those changes. Commented Jul 7, 2016 at 6:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.