6

Can we reload environment variables (process.env) with the updated values without restarting the nodejs app or server ?

In other words, I have created a new environment variable (NEW_VARIABLE) and have set a value in my Windows 7 machine. Now I have written a function to read the environment variable as below in my NodeJS application.

setInterval(() => {
    console.log(process.env.NEW_VARIABLE);
}, 5000);
LarsW
  • 1,514
  • 1
  • 13
  • 25
ram sudheer
  • 139
  • 1
  • 13

2 Answers2

0

Yes you can, see code below where we run 2 loops, one to read and another to increment the variable.

setInterval(() => {
    process.env.NEW_VARIABLE++;
},
1000);

setInterval(() => {
    console.log(process.env.NEW_VARIABLE);
},
1000);

When you update the variable, the next read will be updated.

Unless you are using some packages that has caching behaviour, I have seen that in a few packages.

neo
  • 161
  • 5
  • 4
    Hi Neo, Thanks for quick response. I created the new environment variable in windows from the commandline SET NEW_VARIABLE=test and then run the app with the command "node app". While the app is running, I changed the value of the environment variable SET NEW_VARIABLE=test123. But the function above is still displaying the old value "test" instead of "test123". – ram sudheer Jan 08 '18 at 23:34
0

You should try something like this

process.env.NEW_VARIABLE = process.env.mode + 1;

or

process.env.NEW_VARIABLE = process.env.NEW_VARIABLE++;

This is going to modify it albeit temporary.

dasersoft
  • 155
  • 2
  • 13