13

Is it possible to inject/change the current enviroment variables in an already loaded and started NodeJS process?

Exposing an Interface within the application is not an option, restarting is also not a valid option.

The process is running inside a docker container, requiring a specific NodeJS Version is possible.

EDIT: The change must be done from outside the application source so doing process.env.ENV_VAR = "new env" is not possible.

joachim
  • 652
  • 2
  • 7
  • 23

2 Answers2

12

It isn't possible to modify the env vars of a running process. This isn't unique to NodeJS processes. It's just how env vars work on UNIX like operating systems. The vars live within the address space of the process. And while they are, typically, initially placed at a well known location near the top of the stack the current vars are likely to be at an arbitrary address in the heap. Env vars are intentionally private to each process. So unless the program provides an API for changing its env vars you can't modify them once the program is running.

Kurtis Rader
  • 6,734
  • 13
  • 20
  • So even with the possibility of creating a kernel extension you would still have to guess the address space of that specific var? – joachim Jan 30 '20 at 11:00
  • @joachim That's more or less correct. It's not just finding the var. That's relatively easy because there is a global `environ` var that points to the base of the env vars. The problem is you can't mutate that blob unless you replace a specific var=value with a new string of the same, or shorter, length. You don't need a kernel extension for this. In fact, doing it via a kernel extension would be really hard. You'd want to use a debugger like gdb or lldb. – Kurtis Rader Jan 30 '20 at 19:25
0

You should use a redis store shared between containers that has stored the env.

redis node repo - redis listen for changes