11

I have a react application that connect to micro services. I have different micro services urls per environment. I therefore have multiple env files: .env, .env-develoment-local, env-development,...

When I start the app locally, the app pick up the setting in the .env.development-local which is expecetd.

When I do npm run build I noticed that since it creating a production build, it picks up the .env file.

My question is how can configure the build such a way that it picks other .env files like .env.development or .env.qa, etc... ?

1
  • I am currently using dotenv but I don't know how to build with environment specific .env file. npm run build takes .env build default and I would like to be able to build with say .env.development or .env.qa,... Commented Aug 15, 2019 at 14:38

1 Answer 1

21

I was able to get this working using https://github.com/toddbluhm/env-cmd

I did the following:

npm install --save-dev env-cmd

Then updated package.json accordingly:

"scripts": {
   "start": "react-scripts start",
   "build": "react-scripts build",
   "test": "react-scripts test",
   "eject": "react-scripts eject",
   "build:stage": "env-cmd -f ./.env.stage npm run-script build" 
}

Then run the following command:

npm run build:stage
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. Thanks! Just in case, the "-f" option is very important and it means that we want to specify a custom env file name. Without it, the lib uses a default file ".env".
You don't even need to update package.json file. Just running env-cmd -f .env.stage yarn build works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.