Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ENV variables at runtime

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.

like image 377
joachim Avatar asked Jan 29 '20 16:01

joachim


People also ask

How do I change environment variables in runtime?

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.

Can I change the environment variables without re-building?

Unfortunately, you soon realize that the environment variables have been hard-coded within the build file. This means that you cannot change the environment variables without re-building. Are there any other ways to modify environment variables? We asked around to see if the other projects had a better way to handle it.

How do I set an environment variable in a process?

The SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method lets you define an environment variable that is available to the current process (the Process value). Environment variables that are unique to the current process environment block persist only until the process ends.

Why can't I set environment variables in Unix?

Because .NET on Unix-based systems does not support per-user and per-machine environment variables, only SetEnvironmentVariable (String, String) and SetEnvironmentVariable (String, String, EnvironmentVariableTarget) with a value of EnvironmentVariableTarget.Process successfully store an environment variable to the process environment block.

Are there any environment variables in the browser environment?

First of all, it must be clear that there is no such thing as environment variables inside the browser environment. Whichever solution we use nowadays is nothing but a fake abstraction. But, then you might ask, what about .env files and REACT_APP prefixed environment variables which come straight from documentation?


Video Answer


1 Answers

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.

like image 99
Kurtis Rader Avatar answered Oct 10 '22 06:10

Kurtis Rader