Environments in Create React App

From @jayair on Fri Dec 01 2017 18:45:01 GMT+0000 (UTC)

Link to chapter - https://serverless-stack.com/chapters/environments-in-create-react-app.html

Copied from original issue: https://github.com/AnomalyInnovations/serverless-stack-com/issues/168

It should probably be noted at the top of this section that this tutorial relates to the project completed in Part I.

1 Like

Good point. Iā€™ll add a note.

I am getting this error on windows machine.
ā€˜REACT_APP_STAGEā€™ is not recognized as an internal or external command,
Cant find any related solution.

I think this has to do with the command line format. I donā€™t know the windows version too well. But if you can figure out how to run this on your machine, you should be good.

$ REACT_APP_TEST_VAR=123 npm start

I believe Iā€™m running into the same problem on Windows. Were you able to find a solution?

Thanks!
-Derek

Iā€™m not super familiar with how to set things for Windows. Hopefully somebody can help out.

I was able to get it working using cross-env. I had to install the package and use the syntax below:

ā€œstartā€: ā€œcross-env REACT_APP_STAGE=dev react-scripts startā€,
ā€œbuildā€: ā€œreact-scripts buildā€,
ā€œtestā€: ā€œreact-scripts test --env=jsdomā€,
ā€œpredeployā€: ā€œcross-env REACT_APP_STAGE=dev npm run buildā€,

Thanks!

-Derek

1 Like

Is this for Windows?

If youā€™re in Windows, you should change command to ā€œset REACT_APP_STAGE=xxx && npmā€¦ā€

Hi. I deployed the app in dev and prod environments in AWS S3. Everything OK, until I found that the production app is using dev resources (Cognito, database, etc). Anyone can tell me how to set the REACT_APP_STAGE variable in the S3 bucket?

So the environments are set while building the app. So the REACT_APP_STAGE=prod npm run build is setting it.

I finally solved it having two versions of the .env file (.env.dev and .env.prod) and copy the environment file to .env when deploying to each one.

1 Like

I see. Yeah that works.