Unable to get environment variable with node-config in Window (self answer) - node-config

I'm unable to use node-config to get environment variables in Windows.
My index.js is running a CMD with nodemon.
I've open a new CMD and typed set myApp_jwtSecretKey=mySecretHere
// config/custom-environment-variables.json
{
"jwtSecretKey": "myApp_jwtSecretKey"
}
// index.js
console.log(config.get('myApp_jwtSecretKey'))
// I would expect to see 'mySecretHere' but I get an empty string instead
Why is that?

Hi myself from the past!
The reason you are not getting what you'd expect has to do with the set command of Windows.
Using set the variable is limited to the current command line session.
You need to use setx to set variables permanently and so those can share between command line sessions.
In your case, within your CMD, type setx myApp_jwtSecretKey mySecretHere

Related

Can't find which *FILE* save the environment variable for cshell

When I hit the cmd set in csh it shows me a list of env variables I have right now in my session.
I was wondering where they are set/saved and couldn't find the location.
I have tried in files ~/.cshrc and ~/.cshrc.myusername and in both I saw none of the environment variables that set shows .
Where are they?
In memory. Each instance of csh will get a new copy of your default environment. Setting variables at the command prompt does not persist them anywhere for future sessions.

Get ZSH environment variable in Ruby

I've set an environment variable in my ~/.zshrc file and verified that is shows in the terminal with printenv:
AWS_ACCOUNT_ID=111111111
AWS_ACCESS_KEY_ID=222222222
AWS_SECRET_ACCESS_KEY=AAAAABBBBBBBBB34B3B3B3B3B3B3B3B3B
STRIPE_PUBLISHABLE_KEY=pk_test_1111111111111111
STRIPE_SECRET_KEY=sk_test_222222222222222
I've also checked that I can get the value in irb:
ENV["STRIPE_SECRET_KEY"]
=> "sk_test_222222222222222"
However, I'm getting nothing for the value in my Ruby script. It doesn't return anything. All I'm running at this point is:
key = ENV["STRIPE_SECRET_KEY"]
puts key
Which returns a blank space (not nil) or anything.
What am I missing?
When you run your command with sudo, it's possible the user running the script is not your user account (but depending on the system it could be root).
Therefore, you need to make sure that the user running the script is actually the one you setup the environment variable for.

PyCharm not updating with environment variables

When I use vim to update my environmental variables (in ~/.bashrc), PyCharm does not get the updates right away. I have to shut down the program, source ~/.bashrc again, and re-open PyCharm.
Is there any way to have PyCharm source the changes automatically (or without shutting down)?
When any process get created it inherit the environment variables from it's parent process (the O.S. itself in your case). if you change the environment variables at the parent level, the child process is not aware of it.
PyCharm allows you to change the environment variables from the Run\Debug Configuration window.
Run > Edit Configurations > Environment Variables ->
In my case pycharm does not take env variables from bashrc even after restarting
Pycharm maintains it's own version of environment variables and those aren't sourced from the shell.
It seems that if pycharm is executed from a virtualenv or the shell containing said variables, it will load with them, however it is not dynamic.
the answer below has a settings.py script for the virtualenv to update and maintain settings. Whether this completely solves your question or not i'm not sure.
Pycharm: set environment variable for run manage.py Task
I recently discovered a workaround in windows. Close Pycharm, copy the command to run Pycharm directly from the shortcut, and rerun it in a new terminal window: cmd, cmder, etc.
C:\
λ "C:\Program Files\JetBrains\PyCharm 2017.2.1\bin\pycharm64.exe"
I know this is very late, but I encountered this issue as well and found the accepted answer tedious as I had a lot of saved configurations already.
The solution that a co-worker told me is to add the environment variables to ~/.profile instead. I then had to restart my linux machine and pycharm picked up the new values. (for OSX, I only needed to source ~/.profile and restart pycharm completely)
One thing to be aware is that another coworker said that pycharm would look at ~/.bash_profile so if you have that file, then you need the environment variables added there
In case you are using the "sudo python" technique, be aware that it does not by default convey the environment variables.
To correctly pass on the environment variables defined in the PyCharm launch configuration, use the -E switch:
sudo -E /path/to/python/executable "$#"
This is simply how environment variables work. If you change them you have to re-source your .bashrc (or whatever file the environment variables are located in).
from dotenv import load_dotenv
load_dotenv(override=True)
Python-dotenv can interpolate variables using POSIX variable expansion.
With load_dotenv(override=True) or dotenv_values(), the value of a variable is the first of the values defined in the following list:
Value of that variable in the .env file.
Value of that variable in the environment.
Default value, if provided.
Empty string.
With load_dotenv(override=False), the value of a variable is the first of the values defined in the following list:
Value of that variable in the environment
Value of that variable in the .env file.
Default value, if provided.
Empty string.

Ubuntu: Environment variable is deleted after closing session

I am setting an environment variable in Ubuntu 14.04 for a script to use it.
I opened the terminal and did:
export VARNAME=/home/me/folder/folder2
And then run the script and everything works fine. But anyway as soon as I close my session, the variable seems to disappear and I have to declare it again like the first time.
To set an environment variable that doesn't get erased with the closing of the terminal (Ubuntu 16.04), follow these steps:
Open .bashrc file by using the text editor of your choice. For me, it was
code ~/.bashrc as I use VS Code, but you can use vi ~/.bashrc or subl ~/.bashrc.
Add the environment variable using export VARNAME=/home/me/folder/folder2
Save the file and close.
The variable will persist even after the terminal is closed.
Actually if you set the variable via terminal it will last till shutdown. If you want to set permanent variable you have to do the following.
$ vi ~/.bash_proflle
// set the variable in the file
exit by pressing esc key and type :wq Now the path is set.

Use Environment Variable in the same process that assigned it

I have an Installer that assigns an Environment variable using setx command
Afterwards, that installer invokes a command line that uses this enviroment variable but in that context the variable is still empty.
If I invoke the command line independently the variable is read properly.
Why is that? and how can I overcome that?
I've experimented extensively with SETX. Variables set via SETX cannot be seen in the process or script that sets them, unless you programmatically re-read the pertinent Registry key.

Resources