React package json loading environment variables

Viewed 611

I have a package json file that run the below commands. I can successfully use these environmental variables, however it makes the command very long and ideally it would be better if I load the files through the config instead. Is there a way to load env variables from a file instead of below?

"scripts": {
    "start:staging": "REACT_APP_ENV=staging ENV_2=some_env_variable_2_for_staging ENV_3=some_env_variable_3_for_staging react-scripts start",
    "start:production": "REACT_APP_ENV=staging ENV_2=some_env_variable_2_for_production ENV_3=some_env_variable_3_for_production react-scripts start"
  },
1 Answers

you can create a file with name .env relative to your package.json and put your variables into it.

REACT_APP_ENV_test=test
REACT_APP_ENV_test2=test2

make sure to prefix all your variables with REACT_APP_

Related