3

I have some custom enviroment variables for example: REACT_APP_API_URL='http://localhost:6000/api'

... and different enviroments (develop, test, uat and production).

How can I define custom variables and custom build command for each enviroment?

I found this CRA documentation but it works with Node variables.

froston
  • 1,027
  • 1
  • 12
  • 24
  • This question is not very clear. What is wrong with the example that you have provided? That should work just fine. – Siya Mzam Jun 29 '18 at 14:28

1 Answers1

6

You could install cross-env and use that to set custom REACT_APP_* environment variables for your scripts:

"scripts": {
  "develop": "cross-env REACT_APP_TEST=start react-scripts start",
  "test": "cross-env REACT_APP_TEST=test react-scripts start",
  "uat": "cross-env REACT_APP_TEST=uat react-scripts start",
  "production": "cross-env REACT_APP_TEST=production react-scripts build",
}
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Thank you for your answer. What if I have many enviroment variables? Is it alright to put all of then into the command? – froston Jun 29 '18 at 14:48
  • @froston No problem! Yes, if you have multiple environment variables you can just list them after each other. That is the best option in my opinion, if you don't have that many of them. – Tholle Jun 29 '18 at 14:53
  • 3
    I found a related project [env-cmd](https://github.com/toddbluhm/env-cmd) which allows having these variables in `.env.develop`, `.env.uat` ... files. – froston Jun 29 '18 at 15:09