Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run npm build with specific .env file

Tags:

reactjs

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

like image 988
The Reedemed77 Avatar asked Aug 15 '19 14:08

The Reedemed77


People also ask

Where do I put the .env file?

env file is placed at the base of the project directory. Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable.

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


1 Answers

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
like image 82
The Reedemed77 Avatar answered Oct 26 '22 03:10

The Reedemed77