如何在不重新启动进程的情况下更新进程中的 ENV 变量(NodeJS)? [英] How to update ENV variables in a Process without restarting it (NodeJS)?

查看:50
本文介绍了如何在不重新启动进程的情况下更新进程中的 ENV 变量(NodeJS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台在 NodeJS 上运行的服务器.有没有办法在不重启服务器的情况下更新进程中的环境变量?

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

我想做的是:

  1. 启动我的服务器 npm start
  2. 在控制台输入一些东西来更新 ENV 变量
  3. 服务器使用新的环境变量重新启动

推荐答案

可以调试 Node.js 进程并更改全局变量:

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

在 *nix 上,即使进程未在调试模式下启动,也可以启用调试.在 Windows 上,可以只调试以 node --inspect 启动的进程.本文详细解释了这两种可能性.

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.

显然,这只有在始终直接使用环境变量作为 process.env.FOO 时才有效.

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

如果最初使用它们的值,以后更改 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

这篇关于如何在不重新启动进程的情况下更新进程中的 ENV 变量(NodeJS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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