reload process.env variables without reboot

ram*_*eer 5 environment-variables node.js

Can we reload environment variables (process.env) with the updated values without restarting the nodejs app or server ?

In other words, I have created a new environment variable (NEW_VARIABLE) and have set a value in my Windows 7 machine. Now I have written a function to read the environment variable as below in my NodeJS application.

setInterval(() => {
    console.log(process.env.NEW_VARIABLE);
}, 5000);
Run Code Online (Sandbox Code Playgroud)

小智 0

是的,您可以,请参阅下面的代码,我们运行 2 个循环,一个用于读取,另一个用于递增变量。

setInterval(() => {
    process.env.NEW_VARIABLE++;
},
1000);

setInterval(() => {
    console.log(process.env.NEW_VARIABLE);
},
1000);
Run Code Online (Sandbox Code Playgroud)

当您更新变量时,下一次读取将被更新。

除非您使用一些具有缓存行为的软件包,否则我已经在一些软件包中看到了这一点。

  • 你好,Neo,感谢您的快速回复。我从命令行 SET NEW_VARIABLE=test 在 Windows 中创建了新的环境变量,然后使用命令“node app”运行应用程序。当应用程序运行时,我更改了环境变量 SET NEW_VARIABLE=test123 的值。但上面的函数仍然显示旧值“test”而不是“test123”。 (4认同)