NodeJS - Get environment variables set using cmd - node.js

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?

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.

Related

Deleting an environmental variable for IIS

I've understood that, in order to make IIS read a new environmental variable, I have to reset it via an administrator's command:
iireset /restart
I know that one can also restart IIS from the admin panel at server level.
More context to my complete web app setup in my previous question/answer: in brief it is a Python Flask app running on IIS through WSGI and FastCGI.
What is still surprising me is that now, if I delete a previously created environmental variable, IIS keep seeing it, even after restarting it, as done above.
Why is that possibly happening? And how should I delete the variable for IIS?
If you insist that you want to go with environmental variables (notice that you also have the alternative .env file), then you need to set them via the IIS FastCGI Settings from the IIS admin at server level for your specific application (the one you enabled via wfastcgi-enable).
Edit the specific row as said above and you'll find the Environment Variables collection under the "General" group of FastCGI Properties.
Now you can add and delete any of those variables and it will take effect immediately, without the need of any IIS reboot.

Spring boot is not reading environment variables on Linux server

I am trying to set environment variable for spring boot Datasource related properties.
Datasource properties are in applicationcontext.properties file.
I have tried several approaches to read environment properties which are set on linux server but failed to do so.
i have tried below things inside datasourceset method:
#Autowired private Environment env;
env.getProperty("spring.datasource.username");
or
System.getenv("spring.datasource.username");
But neither of them worked.
Main thing is everything working fine on local windows machine but not working on linux server.
I am able to set env variable on linux even I can see variable is set on env properties list bit whenever i try to read those properties its not returning any value.

nodejs openshift cartridge - not reading custom environment variables

I am identifying my openshift nodejs app environment through system environment variables Ex: 'staging', 'production'. My custom environment variable name is OPENSHIFT_APP_ENV. I have set this in .bash_profile and ran source .bash_profile.
When I did printenv or echo $OPENSHIFT_APP_ENV in command line in my openshift app, I can see the values set properly.
But these variables are not read/set in my nodejs app. I am simply trying to read it as global.ENV = process.env.OPENSHIFT_APP_ENV || "development";
I feel that it should be simple setting issue, but could not get this working somehow. Any help will be appreciated.
You should use the rhc env set command as explained in this section (https://developers.openshift.com/en/managing-environment-variables.html#custom-variables) of the Developer Center to set your environment variables. Especially if you are using a scaled application, that makes sure that your custom environment variables are created on all gears.
It is also standard practice (i believe) to use the NODE_ENV environment variable to determine what environment you are operating in.
You should also make sure to stop & start (not restart) your application after you create environment variables to make sure that your process picks them up correctly (may solve your issue of your application not seeing the ones from your .bash_profile, but i would still recommend using the rhc env set command instead)

Choosing test config and settings in python flask project when running py.test

How to control which _settings.py or config/.py is loaded when starting py.test? I have some defaults set in init.py and some values in development.py. I have tried to load test values from conftest.py but conftest gets executed after the other 2 are loaded. I am specifically looking to change the db being used in development, test and production modes. How to achieve this?
What you are looking for is configuration management in flask. Take a look at this document: flask config management
This SO question could help you too: enter link description here
Basically, depending on your environment (devel / production / test) you change parameter specifying with which settings your app should start. Using system environment variables is one of the option.
app = Flask(__name__)
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
Then, on your devel machine, just set env variable
export YOURAPPLICATION_SETTINGS="/path/to/settings.conf"
and run the application.
On production environment, do the same thing, just change value to /path/to/production/settings.conf

How to set environment variable on ubuntu for nodejs pr app

Is it possible to set environment variables per nodejs app?
The azure module will read the environment variables
AZURE_SERVICEBUS_NAMESPACE and AZURE_SERVICEBUS_ACCESS_KEY for
information required to connect to your Windows Azure Service Bus. If
these environment variables are not set, you must specify the account
information when calling createServiceBusService.
I would like to set different namespaces/key on a per app lvl instead of global?
(i am hosting it in a ubuntu vm)
Not sure if that is possible. But there is something you can do.
In your application just set those environment variables based on others:
process.env.AZURE_SERVICEBUS_NAMESPACE = process.env.<APP>_AZURE_SERVICEBUS_NAMESPACE
process.env.AZURE_SERVICEBUS_ACCESS_KEY = process.env.<APP>_AZURE_SERVICEBUS_ACCESS_KEY
Its a little more verbose but I am not sure if you can set dynamic environment variables.

Resources