How to set environment variable in React JS..?
To set it for current process execution, just edit your package.json file and modify the "build" script as follows:
"scripts": {
"start": "react-scripts start",
"build": "set \"CI=false\" && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject" }
This will set CI environment variable to "false". Now you can execute the build command with CI variable set:
npm run build
check out this package dotenv,
create a new file
.env
in your working directoryinstall
dotenv
bynpm install dotenv
add this to your app
require('dotenv').config()
in that file write
process.env.CI = false
add
.env
to your.gitignore
[if using git]restart your app.
OR run this CI=false npm run build
Your question title is very different to what is happening in the description.
To use environment variables in React, they must be prefixed with REACT_APP_
.
For example, the following will be picked up by a React application:
REACT_APP_API_URL=/api
Whereas this won't:
API_URL=/api
For more, please see the official documentation: