24

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 as errors because process.env.CI = true.
Most CI servers set it automatically.

Failed to compile.



./src/Home.js
  Line 2:   'AppNavbar' is defined but never used  no-unused-vars
  Line 3:  'Link' is defined but never used       no-unused-vars
  Line 4:  'Button' is defined but never used     no-unused-vars
  Line 4:  'Container' is defined but never used  no-unused-vars

./src/App.js
  Line 5:   'MenuBar' is defined but never used        no-unused-vars
  Line 6:   'PrivilegeList' is defined but never used  no-unused-vars
  Line 8:   'logo' is defined but never used           no-unused-vars


  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! [email protected] build: `react-scripts build`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the [email protected] build script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

  npm ERR! A complete log of this run can be found in:
    npm ERR!     D:\ReactJS-workspace\my-app\npm\cache\_logs\2018-10-19T07_44_19_233Z-debug.log
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 01:36 min
    [INFO] Finished at: 2018-10-19T13:14:19+05:30
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (npm run build (compile)) on project my-app: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Below is my folder structure.

enter image description here

I want to set process.env.CI = false how to set environment variable in React JS?

6
  • Well I think to do that you just need to disable the task which does the eslint or jshint step. Commented Oct 19, 2018 at 8:09
  • Which script is being executed by CI ? Can you share the script Commented Oct 19, 2018 at 8:10
  • 2
    The easiest fix is to remove the unused imports. Commented Oct 19, 2018 at 8:10
  • @Code-Appretice Yeah I removed unused imports it fixed error thanks Commented Oct 23, 2018 at 7:05
  • Simply CI=false npm run build without no change. Commented Jan 29, 2020 at 13:24

12 Answers 12

23

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
Sign up to request clarification or add additional context in comments.

4 Comments

Not worked for me, i am using Travis CI, it shows me ,Treating warnings as errors because process.env.CI = true. Most CI servers set it automatically.
@AshishKamble did you find a solution ?
@TeddyKossoko Not Actually, sry
@AshishKamble Try CI=false npm run build
14

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

6 Comments

Do I need to add require('dotenv').config() in every js file..?
You mean to say App.js right...! and also abt 5 th point under #misc section I should add .env..?
yes App.js, and 5th point is if you're using git otherwise ignore it
Does not resolve it above error still getting same error
run this CI=false npm run build once
|
11

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:

Comments

8

You can set environment variables in .env file

Here are the steps

  1. create a file with the name of .env in the project root folder

  2. Now, at the time to add a variable you have to add prefix REACT_APP for e.g: You want to add API_URL variable for your API. So you have to add a variable with prefix RECT_APP as below

    REACT_APP_API_URL

  3. Stop your running server and re-run using npm start

  4. To access env variable you have to call like: process.env.REACT_APP_API_URL

Here you go. Now you can access the env variable

Note:

  1. Please make sure you have added prefix (REACT_APP)
  2. Please stop your server and restart so it will load env variable if added

1 Comment

Good detail and step by step guide. Emphasis on #3: you must stop your server and restart it! This fixed my issue.
3
"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

1 Comment

I had a similar problem working with React and Azure Static Web App CI/CD and "build": "CI=false react-scripts build", it works to me, Deployment Complete :)
2

You have to create .env file in the root directory and define variable in that file. Please make sure each variable starts 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

Comments

1

Create a .env file or else .env.dev,.env.qa,.env.stg ...etc

Add the following line to that file

CI=false

if you haven't already installed env-cmd, first install it and include it in package.json

Then add the following lines inside "scripts" of package.json

  "scripts": {
    "start": "env-cmd -f .env.dev react-scripts start",
    "build": "react-scripts build",
    "build:dev": "env-cmd -f .env.dev npm run-script build",
    "build:qa": "env-cmd -f .env.qa npm run-script build",
    "build:stg": "env-cmd -f .env.stg npm run-script build",
    "build:prod": "env-cmd -f .env.prod npm run-script build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

Then build using "npm run build:dev" or with the command accordingly

Comments

1

For Windows

set REACT_APP_ENV=development && react-scripts start

For Linux, Ubuntu, macOs

REACT_APP_ENV=development react-scripts start

Comments

0

It looks like you need your app to have access to process.env variables.

To do that, you have several options (one of which includes using a third party library above, which is a good option, but it's doing multiple things).

1) Set environment variables in your launch command, Example: CI=travis npm start. In this case, you'll have access to process.env.CI in your app.

2) Set environment variable in your, you know, environment. If you're on Mac or Linux, simply add an environment variable, as you normally would that your shell will export. Check with echo $VAR

3) Manually do something stupid in your app to write to globals. Probably don't bother.

4) Just use .dotenv. What it does isn't really complicated, but it offers a solution that is almost must have on most projects, for multiple reasons.

Comments

0

I arrived at this questions from searching for integration with Bitbucket Pipelines.

For anyone else looking for the same, you can add CI as false under Settings/Repository Variables for your repo (if you don't want it being part of your version controlled code).

If you want to add it for all your repos in your bitbucket, you would add in your global settings, but probably best to add this one on a repo by repo basis.

Comments

0

Github Actions

Trying to build and upload website using Github actions?

Maybe you want to create a file in your Github repo \.github\workflows\deploy.yaml and add something like this.

name: Upload Website

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: Upload Website - deploy
    runs-on: ubuntu-latest

    steps:
      - name: Checkout source code
        uses: actions/checkout@master

      - name: Load AWS credentials from Github Secrets
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Load dependencies (node_modules)
        run: yarn

      - name: Build site
        run: yarn build
        env:
          CI: false

      - name: Copy files to S3
        run: |
          aws s3 sync ./build s3://my-s3-website-bucket --delete --exclude "*.map" --acl public-read

      - name: Clear Cloudfront cache
        run: |
          aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DIST_ID }}--paths "/*"

References:

Comments

-1

Create a file with name .eslintrc in your root folder and add below rules in that file -

{
    "rules": {
        "no-unused-vars": "off"
    }
}

This will disable the strict check for eslint rule no-unused-vars. You can add more rules in this file if you want to disable those also.

For more details follow the guide - https://eslint.org/docs/user-guide/configuring

7 Comments

Hi Ashivn it give me enoent ENOENT: no such file or directory, open 'D:\ReactJS-workspace\package.json' error
What do you mean by it?
I put .eslintrc in D:\ReactJS-workspace\my-app folder which is root folder of my application it giving above error
You got this error as soon as you have added this file or it is coming while running some script like npm start ? Also can you check paths, both looks different D:\\React-JSWorkspace\my-app 'D:\ReactJS-workspace\package.json
I added .eslintrc file in root folder of and running "mvn clean install" to build war it give me error above
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.