Search code examples
javascriptwebpackenvironment-variablesdotenv

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.

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.

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. 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. I already set the systemvars to true to detect all environment variables from the system or .env file.

=====

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.


Solution

  • You simply cant change environment variable. Evn variables loaded on compile/webpack load time. So once app start u cant change, process.env.

    Solution as @jonrsharpe explains. 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.

    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. This is in memory-based.