Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load different env variables with .env and create-react-app

I need to load for each development/production server a different .env file.

Dev/Prod Server     | .env file
---------------------------------------------
localhost           | .env.development.local
development         | .env.development
qa                  | .env.qa
production          | .env.production

So, I tried to edit the package.json in this way

"start-js": "react-scripts start .env.development.local",
"start": " npm-run-all -p watch-css start-js",
"build:dev": "npm run build-css && react-scripts build .env.development",
"build:qa": "npm run build-css && react-scripts build .env.qa",
"build:pro": "npm run build-css && react-scripts build .env.production"

but if I execute a build:* command, the file loaded is always .env.production because the NODE_ENV is production. Is it possibile to load the others .env file with "build:* command?

like image 283
Webman Avatar asked Aug 11 '26 23:08

Webman


1 Answers

I solved my question using react-app-env and renaming the .env file.

Dev/Prod Server     | .env file
---------------------------------------------
qa                  | qa.env

And the build:qa scripts, for example, became:

"build": "npm run build-css && react-scripts build",
"build:qa": "react-app-env --env-file=qa.env build"
like image 170
Webman Avatar answered Aug 14 '26 20:08

Webman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!