The Reedemed77
The Reedemed77

Reputation: 459

Run npm build with specific .env file

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... ?

Upvotes: 11

Views: 16959

Answers (1)

The Reedemed77
The Reedemed77

Reputation: 459

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

Upvotes: 21

Related Questions