Skip to content Skip to sidebar Skip to footer

How To Set Environment Variable In React JS..?

I am new to React JS. I am trying to build war file from React App but stuck somewhere below. It gives me below errors. Creating an optimized production build... Treating warnings

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,

  1. create a new file .env in your working directory

  2. install dotenv by npm install dotenv

  3. add this to your app require('dotenv').config()

  4. in that file write process.env.CI = false

  5. add .env to your .gitignore [if using git]

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

Reference


Post a Comment for "How To Set Environment Variable In React JS..?"