How To Set Environment Variable In React JS..?
Solution 1:
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
Solution 2:
check out this package dotenv,
create a new file
.envin your working directoryinstall
dotenvbynpm install dotenvadd this to your app
require('dotenv').config()in that file write
process.env.CI = falseadd
.envto your.gitignore[if using git]restart your app.
OR run this CI=false npm run build
Solution 3:
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:
Solution 4:
"scripts": {
"start": "react-scripts start",
"build": "CI=false react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Try this ...This will set the CI to false
Solution 5:
You have to create .env file in the root directory and define variable in that file. Please make sure each varibale start with REACT_APP_ like REACT_APP_IS_PROD=1
You have to restart the server every time when you change or create a new variable.
Post a Comment for "How To Set Environment Variable In React JS..?"