Environment variables are variables which are present outside the project and embedded into the project during build time. The CRA generates static HTML/CSS/JS bundles, hence it cannot read them at runtime.
Note: Environment variables are explained for React project created using
create-react-app
.
A project can have sensitive information which cannot be shared by another person and can be potentially misused. A few examples of sensitive information are secret or private keys for an API service like Firebase, MailJet and other services which can be used in the project.
We require multiple environments like development, production, test, and UAT. Each environment can have a different configuration for various features like Database, deployment URL, and secret keys for various services.
.env
We can define the environment variables for react project by creating a .env
file at the root of the project.
All the environment variables should always start with REACT_APP_
, Otherwise it will be ignored.
Note: The development server must be restarted after changing the
.env
files. Similarly, the code needs to rebuild to update the build code with new environment configuration changes.
There is an example of a .env
file.
REACT_APP_PROJECT_TITLE = React Project - Dev
REACT_APP_API_CLIENT_BASE_URL = https://api.dev.project.com
public/index.html
)Add %
before and after the environment variable name to access the value.
<title>%REACT_APP_PROJECT_TITLE%</title>
.js
, .jsx
, .ts
, .tsx
)
const baseURL = process.env.REACT_APP_API_CLIENT_BASE_URL;
.env
The selection of environment files changes depending on the script used in React project. The priority of environment files decreases from left to right.
npm start
runs the app in development
mode.
.env.development.local
, .env.local
, .env.development
, .env
npm run build
builds the app for production to the build
folder.
.env.production.local
, .env.local
, .env.production
, .env
npm test
runs the app in test
mode.
.env.test.local
, .env.test
, .env
Local variations of
.env
which are.env.local
,.env.development.local
,.env.test.local
,.env.production.local
should not be source controlled.
By now, we know that CRA provides to have development
, production
and testing
.
Here are a few scenarios where you would need to create custom scripts.
This problem is solved by using an NPM package
env-cm
which allows the creation of scripts using an environment from a.env
file.
env-cm
NPM package
npm i env-cmd
Let us assume I have a new environment called UAT
. First, we have to create .env.uat
file.
REACT_APP_PROJECT_TITLE = React Project - UAT
REACT_APP_API_CLIENT_BASE_URL = https://api.uat.project.com
package.json
The command to run a script with a preferred environment file
env-cmd -f <ENVIRONMENT-FILE-NAME> <SCRIPT-TO-RUN>
The scripts in the below code snippet are used to create an optimized production build using the respective environment files.
"build:production": "env-cmd -f .env.production react-scripts build",
"build:uat": "env-cmd -f .env.uat react-scripts build",
UAT
environment
npm run build:uat
npm install -g serve
serve -s build