简体   繁体   English

Dotenv Webpack - 当我在运行时更改系统环境变量时,更改不会影响应用程序

[英]Dotenv Webpack - When I change the system environment variables on runtime, the changes is not affected on the app

I build a web app using react, webpack, docker and AWS.我使用 react、webpack、docker 和 AWS 构建了一个 Web 应用程序。

I create a feature which depends on environment variable.我创建了一个依赖于环境变量的功能。 So if the environment variable value is true, the feature will be showing on the frontend.因此,如果环境变量值为 true,则该功能将显示在前端。

The problem is, when I changed the environment variable on the server (Webpack already done building the app, the app is already deployed and running), my feature is not showing.问题是,当我更改服务器上的环境变量时(Webpack 已经完成了应用程序的构建,应用程序已经部署并运行),我的功能没有显示。 I guess due to the app cannot read the value changes on system environment variable.我猜是由于应用程序无法读取系统环境变量的值更改。

How can I achieve this ?我怎样才能做到这一点? is it possible to do it ?有可能做到吗?

===== ======

I use dotenv webpack for managing my environment variables.我使用dotenv webpack来管理我的环境变量。 I already set the systemvars to true to detect all environment variables from the system or .env file.我已经将systemvars设置为true以检测系统或 .env 文件中的所有环境变量。

===== ======

So why am I doing this, because I dont want to make a Pull Request to push a new value for environment variables.那我为什么要这样做,因为我不想发出拉取请求来为环境变量推送新值。 I just want to reserve the environment variables name, and change the value directly from the server if the feature is ready to deployed.我只想保留环境变量名称,如果该功能已准备好部署,则直接从服务器更改值。 And if there is an error, I just need to change the environment variable to something and the feature is down.如果出现错误,我只需要将环境变量更改为某些内容,然后该功能就会关闭。

You simply cant change environment variable.您根本无法更改环境变量。 Evn variables loaded on compile/webpack load time.在编译/webpack 加载时加载的 Evn 变量。 So once app start u cant change, process.env .所以一旦应用程序启动你就不能改变, process.env

Solution as @jonrsharpe explains. @jonrsharpe 解释的解决方案。 You need to create, some sort of database.您需要创建某种数据库。 It could be memory or file or database.它可以是内存、文件或数据库。 You read data from that.你从中读取数据。 Expose a API, to update the data base.公开 API,以更新数据库。

Express sample:快递样品:

global.enableFeature = false

app.post("/updateFeatureToggle", (req, res) => {
  const enableFeature = req.body.enableFeature

  global.enableFeature = enableFeature
  res.send({success: "OK"})
})

In another file, read from global.enableFeature .在另一个文件中,从global.enableFeature读取。 This is in memory-based.这是基于内存的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM