-2

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?

pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • 1
    Does this answer your question? [create react app not picking up .env files?](https://stackoverflow.com/questions/48378337/create-react-app-not-picking-up-env-files) – Nasiruddin Saiyed May 10 '22 at 14:24
  • No, that's totally different. I just want to read the env values from a json, not from the .env – pmiranda May 11 '22 at 20:00

1 Answers1

0

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);
Abir Taheer
  • 2,502
  • 3
  • 12
  • 34
  • But I wont add to package.json the enviroment var, I want to use a file called (for instance) .env.json add it to .gitignore, and then put a json there, so I want to read that json instead the default .env file. Other wise, I will keep converting manually the json to .env – pmiranda May 11 '22 at 19:58
  • @pmiranda check now – Abir Taheer May 12 '22 at 06:21
  • But I can't put in .gitignore `package.json`. My idea is to had that json ignored – pmiranda May 12 '22 at 15:18