'React, use JSON as env

I have a shared JSON on vault for a React app like this one:

{
    "REACT_APP_SOME_ENV": "some value",
    "REACT_APP_SOME_ENV2": "some value2",
}

How could I use it as the .env file?

Right now I'm just copy pasting each value as a regular .env file:

REACT_APP_SOME_ENV=some value
REACT_APP_SOME_ENV2=some value2

But that's not the idea, that file changes a lot.

Any npm tools or something?



Solution 1:[1]

To add values from a JSON to your environment just require() the JSON file and assign its values to process.env

Example env.json

{
    "REACT_APP_SOME_ENV": "some value",
    "REACT_APP_SOME_ENV2": "some value2"
}

In your index.js file:

const envJSON = require("./env.json");

Object.assign(process.env, envJSON);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1