Search code examples
reactjsnpmcreate-react-app

How to use create-react-app build with custom enviroment variables?


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.


Solution

  • 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",
    }