在运行时更改ENV变量 [英] Change ENV variables at runtime

查看:71
本文介绍了在运行时更改ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在已经加载并启动的NodeJS进程中注入/更改当前环境变量?

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.

该进程正在docker容器中运行,可能需要特定的NodeJS版本.

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

必须从应用程序源外部进行更改,因此无法进行 process.env.ENV_VAR ="new env" .

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

推荐答案

无法修改正在运行的进程的环境变量.这并不是NodeJS流程所独有的.这就是env vars在UNIX之类的操作系统上的工作方式.变量位于进程的地址空间内.尽管通常将它们最初放置在靠近堆栈顶部的众所周知的位置,但当前的var可能位于堆中的任意地址.Env var是每个过程有意识地私有的.因此,除非程序提供用于更改其环境变量的API,否则程序运行后就无法对其进行修改.

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.

这篇关于在运行时更改ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆