11

I have a server running on NodeJS. Is there a way to update the environment variables in the process without restarting the server?

What I'm looking to do is:

  1. Start my server npm start
  2. type something into the console to update ENV variable
  3. Server restarts with new environment variable
nikjohn
  • 20,026
  • 14
  • 50
  • 86

6 Answers6

6

From a programmatic standpoint, you should be able to update the process.env variable that was passed into the running process.

For example, running:

cmd_line$: MY_VALUE=some_val node ./index.js

with code:

console.log(process.env.MY_VALUE)
process.env.MY_VALUE = 'some other value'
console.log(process.env.MY_VALUE)
process.env.MY_VALUE = 4
console.log(process.env.MY_VALUE)

output in terminal:

some_val
some other value
4

From a server admin standpoint for an already running application, I don't know the answer to that.

jsadler
  • 109
  • 6
5

It's possible to debug Node.js process and change global variables:

debug

On *nix, it's possible to enable debugging even if a process wasn't started in debug mode. On Windows, it's possible to debug only processes that were started with node --inspect. This article explains both possibilities in detail.

Obviously, this will work only if environment variables are used directly all the time as process.env.FOO.

If their values are initially used, changing process.env.FOO later may not affect anything:

const FOO = process.env.FOO;
...
console.log(FOO); // it doesn't matter whether process.env.FOO was changed at this point
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
3

If you make a change to env variables they will take place immediately only if you make the change via the main Properties dialog for the system which is going to my computer -> Advanced properties -> Environment Variables.

Any program which is already running will not see the changes unless we handle it in the code explicitly.

Logic behind it is that there is an agent which sends a broadcasting a WM_SETTINGCHANGE message and make changes to all applications inorder to notify for that change.

LearningToCode
  • 392
  • 5
  • 18
  • 1
    This is kind of what I want, but I am looking to see if there is a way to do this on the console directly – nikjohn Jul 23 '18 at 18:25
0

Here's a good explanation on how to setup environment variables in mac OS. Additionally, as others have already answered, you can set them with process.env.ANY_VARIABLE = 'some value'.

Rodrigo Mata
  • 1,779
  • 2
  • 14
  • 23
0

I am trying to find a cross-platform answer to this (mac, linux, windows)

For mac, a workaround that I found is using

const shell = require("shelljs");  

console.log(shell.exec('launchctl getenv TEST'));

It only works if you set environment variable (e.g. in other terminal tab) using launchctl setenv TEST blahblah

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
0

You can try to use this javascript tiny library to update env variables without restarting the node server:

https://www.npmjs.com/package/runtime-node-refresh

Hope this help.

Przemek Nowicki
  • 572
  • 4
  • 7