How to fix build failures with `create-react-app` in production

Dear sir,

I’m getting message site deploy failed, even I did CI= run npm build and this error is coming almost for all my react project. Kindly help me out, I will be really gratefull to you.
Github project link:
https://github.com/satish-Maurya-4/amazonClone

Netlify link:

Hi @Satish_M4,

Your log says this:

Cannot find module: 'react-icons/bs'. Make sure this package is installed.

If I go to your package.json: https://github.com/satish-Maurya-4/amazonClone/blob/master/package.json, I can see that the dependency doesn’t exist. You need to add the dependency to package.json.

Dear sir,
thanks for the reply,

I.m getting following error in order to add dependency,
as per my opinion it’s a part of react icons package and in design the icon bsSearch is working, may be it can be different issue.
help me out sir

Hi @Satish_M4,

You need to run npm i react-icons.

See here:

Then you can easily import icons from react-icons/bs.

the obvious problem of your issues β†’ there is a lot of unused vars on your application file structure for sure it’s gonna have an error. your application must be ready means there is no unused vars on your application.

Just in case someone ends up on this thread I had this issue as well.
I want to add that the fist solution offered by @ehmicky worked for me.
I had a warning that one of my useEffects did not have anything in the dependency array. Not a breaking error, just a warning. However, apparently it triggered failure in Netlify build.
I resolved the error, and the build was successful.

1 Like

The truth is, I have looked many times and no, I do not have any dynamic amount, I looked in other forums and they told me that it could be a loose file but neither. I’ve looked file by file and something is going wrong and I can’t figure it out. I need to identify the error but I can’t. Yesterday I was 11h with this, today I’ve been all day. Please, I want to get this problem off my back.

my imports

import React, { useEffect, useState } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import Header from './components/Header/Header'
import Webhooks from './components/Webhooks/Webhooks';
import ServerConfig from './components/ServerConfig/ServerConfig';
import ShopifySites from './components/ShopifySites/ShopifySites';
import Payment from './components/Payment/Payment';
import Login from './components/Login/Login';
import styled, {ThemeProvider} from "styled-components";
import {lightTheme, darkTheme, GlobalStyles} from './themes';
import './App.css'

my package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.4",
    "@testing-library/user-event": "^13.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^6.2.2",
    "react-scripts": "^5.0.0",
    "styled-components": "^5.3.3",
    "web-vitals": "^2.1.4",
    "yarn": "^1.22.18"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Hey @Marc4567,

This doesn’t seem to be perfectly related to the thread above. Yes, the thread talks about build errors with CRA, but your error is a unique one so far. Furthermore, it doesn’t seem possible to have any idea with the shared information. Would you be able to share a reproduction case?

Hi, I have tried many ways to fix this issue, but still not working. Here is my deploy log:

11:49:43 PM: build-image version: d7b3813f01c06610bc1723ff1b22446513ee7941 (focal)
11:49:43 PM: build-image tag: v4.14.3
11:49:43 PM: buildbot version: f2613f2234a773b4fab58b5a3c03c743fe78f9bf
11:49:43 PM: Fetching cached dependencies
11:49:43 PM: Starting to download cache of 104.2MB
11:49:44 PM: Finished downloading cache in 1.052557149s
11:49:44 PM: Starting to extract cache
11:49:45 PM: Finished extracting cache in 1.175511964s
11:49:45 PM: Finished fetching cache in 2.279816179s
11:49:45 PM: Starting to prepare the repo for build
11:49:45 PM: Preparing Git Reference refs/heads/master
11:49:47 PM: Parsing package.json dependencies
11:49:48 PM: Starting build script
11:49:48 PM: Installing dependencies
11:49:48 PM: Python version set to 2.7
11:49:48 PM: Started restoring cached node version
11:49:49 PM: Finished restoring cached node version
11:49:49 PM: v16.18.0 is already installed.
11:49:49 PM: Now using node v16.18.0 (npm v8.19.2)
11:49:50 PM: Enabling node corepack
11:49:50 PM: Started restoring cached build plugins
11:49:50 PM: Finished restoring cached build plugins
11:49:50 PM: Attempting ruby version 2.7.2, read from environment
11:49:50 PM: Using ruby version 2.7.2
11:49:50 PM: Using PHP version 8.0
11:49:51 PM: No npm workspaces detected
11:49:51 PM: Started restoring cached node modules
11:49:51 PM: Finished restoring cached node modules
11:49:51 PM: Started restoring cached go cache
11:49:51 PM: Finished restoring cached go cache
11:49:51 PM: Installing Go version 1.17 (requested 1.17)
11:49:56 PM: unset GOOS;
11:49:56 PM: unset GOARCH;
11:49:56 PM: export GOROOT=β€˜/opt/buildhome/.gimme/versions/go1.17.linux.amd64’;
11:49:56 PM: export PATH=β€œ/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}”;
11:49:56 PM: go version >&2;
11:49:56 PM: export GIMME_ENV=β€œ/opt/buildhome/.gimme/env/go1.17.linux.amd64.env”
11:49:56 PM: go version go1.17 linux/amd64
11:49:56 PM: Installing missing commands
11:49:56 PM: Verify run directory
11:49:57 PM: ​
11:49:57 PM: ────────────────────────────────────────────────────────────────
11:49:57 PM: Netlify Build
11:49:57 PM: ────────────────────────────────────────────────────────────────
11:49:57 PM: ​
11:49:57 PM: ❯ Version
11:49:57 PM: @netlify/build 28.1.4
11:49:57 PM: ​
11:49:57 PM: ❯ Flags
11:49:57 PM: baseRelDir: true
11:49:57 PM: buildId: 635cccfbab13d21f45675219
11:49:57 PM: deployId: 635cccfbab13d21f4567521b
11:49:57 PM: ​
11:49:57 PM: ❯ Current directory
11:49:57 PM: /opt/build/repo
11:49:57 PM: ​
11:49:57 PM: ❯ Config file
11:49:57 PM: No config file was defined: using default values.
11:49:57 PM: ​
11:49:57 PM: ❯ Context
11:49:57 PM: production
11:49:57 PM: ​
11:49:57 PM: ────────────────────────────────────────────────────────────────
11:49:57 PM: 1. Build command from Netlify app
11:49:57 PM: ────────────────────────────────────────────────────────────────
11:49:57 PM: ​
11:49:57 PM: $ CI= npm run build
11:49:57 PM: > california-thai@0.1.0 build
11:49:57 PM: > react-scripts build
11:49:58 PM: Creating an optimized production build…
11:49:59 PM: Failed to compile.
11:49:59 PM:
11:49:59 PM: Module not found: Error: Can’t resolve β€˜react-hook-form’ in β€˜/opt/build/repo/src/components’
11:49:59 PM: ​
11:49:59 PM: ────────────────────────────────────────────────────────────────
11:49:59 PM: β€œbuild.command” failed
11:49:59 PM: ────────────────────────────────────────────────────────────────
11:49:59 PM: ​
11:49:59 PM: Error message
11:49:59 PM: Command failed with exit code 1: CI= npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
11:49:59 PM: ​
11:49:59 PM: Error location
11:49:59 PM: In Build command from Netlify app:
11:50:00 PM: Creating deploy upload records
11:49:59 PM: CI= npm run build
11:49:59 PM: ​
11:49:59 PM: Resolved config
11:49:59 PM: build:
11:49:59 PM: command: CI= npm run build
11:50:00 PM: Failed during stage β€˜building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
11:49:59 PM: commandOrigin: ui
11:49:59 PM: environment:
11:49:59 PM: - CI
11:49:59 PM: publish: /opt/build/repo/build
11:49:59 PM: publishOrigin: ui
11:49:59 PM: Caching artifacts
11:49:59 PM: Started saving node modules
11:49:59 PM: Finished saving node modules
11:49:59 PM: Started saving build plugins
11:49:59 PM: Finished saving build plugins
11:49:59 PM: Started saving pip cache
11:50:00 PM: Finished saving pip cache
11:50:00 PM: Started saving emacs cask dependencies
11:50:00 PM: Finished saving emacs cask dependencies
11:50:00 PM: Started saving maven dependencies
11:50:00 PM: Finished saving maven dependencies
11:50:00 PM: Started saving boot dependencies
11:50:00 PM: Finished saving boot dependencies
11:50:00 PM: Started saving rust rustup cache
11:50:00 PM: Finished saving rust rustup cache
11:50:00 PM: Started saving go dependencies
11:50:00 PM: Finished saving go dependencies
11:50:00 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:50:00 PM: Failing build: Failed to build site
11:50:00 PM: Finished processing build request in 17.202916844s

Hi @Howard

As stated in the line above, react-hook-form doesn’t exist, hence the build error.

One suggestion is checking the filename as the build system is case sensitive.

Failing that, please share the public repository you are deploying from.

Please Please Please
What should I do?

Apologies @Howard, my last response was a little off.

In components/Form.js you have

import { useForm } from "react-hook-form";

however react-hook-form isn’t listed as a dependency in the project’s package.json. You need to add it.

Thank you this solved my issue, i had to add CI= npm run build and worked !

I’ve been having a similar issue but none of these solutions have helped…my app keeps failing and I’m getting the same fail every time but I’m not sure what the issue is anymore.

1:43:08 PM: build-image version: b99b04bce77f7c739cbd5eebb242ff3ddb54ba7b (focal)
1:43:08 PM: buildbot version: b99b04bce77f7c739cbd5eebb242ff3ddb54ba7b
1:43:08 PM: Fetching cached dependencies
1:43:08 PM: Failed to fetch cache, continuing with build
1:43:08 PM: Starting to prepare the repo for build
1:43:08 PM: No cached dependencies found. Cloning fresh repo
1:43:08 PM: git clone --filter=blob:none GitHub - TaylorCoppedge/FuryFriendsGalleryII: For Personal Use
1:43:08 PM: Preparing Git Reference refs/heads/TaylorCoppedge-patch-1
1:43:09 PM: Parsing package.json dependencies
1:43:10 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: β€˜build’ versus β€˜β€™ in the Netlify UI
1:43:10 PM: Starting to install dependencies
1:43:10 PM: Started restoring cached go cache
1:43:10 PM: Finished restoring cached go cache
1:43:10 PM: Installing Go version 1.19.5 (requested 1.19.5)
1:43:15 PM: go version go1.19.5 linux/amd64
1:43:15 PM: Using PHP version 8.0
1:43:15 PM: v16.19.1 is already installed.
1:43:16 PM: Now using node v16.19.1 (npm v8.19.3)
1:43:16 PM: Enabling Node.js Corepack
1:43:16 PM: Started restoring cached build plugins
1:43:16 PM: Finished restoring cached build plugins
1:43:16 PM: Python version set to 3.8
1:43:16 PM: Attempting Ruby version 2.7.2, read from environment
1:43:17 PM: Using Ruby version 2.7.2
1:43:18 PM: Install dependencies script success
1:43:18 PM: Starting build script
1:43:18 PM: Using stage install dependencies
1:43:18 PM: Detected 1 framework(s)
1:43:18 PM: β€œcreate-react-app” at version β€œunknown”
1:43:18 PM: Section completed: initializing
1:43:20 PM: ​
1:43:20 PM: Netlify Build
1:43:20 PM: ────────────────────────────────────────────────────────────────
1:43:20 PM: ​
1:43:20 PM: ❯ Version
1:43:20 PM: @netlify/build 29.8.0
1:43:20 PM: ​
1:43:20 PM: ❯ Flags
1:43:20 PM: baseRelDir: true
1:43:20 PM: buildId: 64232729bdd823095269e00b
1:43:20 PM: deployId: 64232729bdd823095269e00d
1:43:20 PM: ​
1:43:20 PM: ❯ Current directory
1:43:20 PM: /opt/build/repo/build
1:43:20 PM: ​
1:43:20 PM: ❯ Config file
1:43:20 PM: No config file was defined: using default values.
1:43:20 PM: ​
1:43:20 PM: ❯ Context
1:43:20 PM: production
1:43:20 PM: ​
1:43:20 PM: 1. Build command from Netlify app
1:43:20 PM: ────────────────────────────────────────────────────────────────
1:43:20 PM: ​
1:43:20 PM: $ CI= npm run build
1:43:20 PM: > furry-friends-two@0.1.0 build
1:43:20 PM: > react-scripts build
1:43:20 PM: sh: 1: react-scripts: not found
1:43:20 PM: ​
1:43:20 PM: β€œbuild.command” failed
1:43:20 PM: ────────────────────────────────────────────────────────────────
1:43:20 PM: ​
1:43:20 PM: Error message
1:43:20 PM: Command failed with exit code 127: CI= npm run build (Search results for '"non-zero exit code: 127"' - Netlify Support Forums)
1:43:20 PM: ​
1:43:20 PM: Error location
1:43:20 PM: In Build command from Netlify app:
1:43:21 PM: Failed during stage β€˜building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
1:43:20 PM: CI= npm run build
1:43:20 PM: ​
1:43:20 PM: Resolved config
1:43:20 PM: build:
1:43:20 PM: base: /opt/build/repo/build
1:43:20 PM: command: CI= npm run build
1:43:20 PM: commandOrigin: ui
1:43:20 PM: environment:
1:43:20 PM: - REACT_APP_DOG_API_KEY
1:43:20 PM: - REACT_APP_DOG_API_URL
1:43:20 PM: publish: /opt/build/repo/build
1:43:20 PM: publishOrigin: default
1:43:20 PM: Caching artifacts
1:43:20 PM: Started saving build plugins
1:43:20 PM: Finished saving build plugins
1:43:20 PM: Started saving pip cache
1:43:20 PM: Finished saving pip cache
1:43:20 PM: Started saving emacs cask dependencies
1:43:20 PM: Finished saving emacs cask dependencies
1:43:20 PM: Started saving maven dependencies
1:43:20 PM: Finished saving maven dependencies
1:43:20 PM: Started saving boot dependencies
1:43:20 PM: Finished saving boot dependencies
1:43:20 PM: Started saving rust rustup cache
1:43:20 PM: Finished saving rust rustup cache
1:43:20 PM: Started saving go dependencies
1:43:20 PM: Finished saving go dependencies
1:43:21 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
1:43:21 PM: Failing build: Failed to build site
1:43:21 PM: Finished processing build request in 13.266s

This is the git repository for the build.

The GitHub repository you shared @TaylorCoppedge contains no React project, only the output of a React project in the build directory. This indicated you have not pushed the base project code to GitHub.

If this is what you intended, there is no need to have build command set in Netlify, you need only set the publish directory to build.

If this is not what you intended, you need to commit all other project files and push them to GitHub in order to have Netlify build the project.

so I actually redid the project and it’s no longer giving me that error code but it’s now saying

4:50:38 PM: build-image version: b99b04bce77f7c739cbd5eebb242ff3ddb54ba7b (focal)
4:50:38 PM: buildbot version: b99b04bce77f7c739cbd5eebb242ff3ddb54ba7b
4:50:38 PM: Fetching cached dependencies
4:50:38 PM: Failed to fetch cache, continuing with build
4:50:38 PM: Starting to prepare the repo for build
4:50:38 PM: No cached dependencies found. Cloning fresh repo
4:50:38 PM: git clone --filter=blob:none GitHub - TaylorCoppedge/FurryFriendsGallery2: Testing my new build
4:50:38 PM: Preparing Git Reference refs/heads/main
4:50:39 PM: Parsing package.json dependencies
4:50:40 PM: Starting to install dependencies
4:50:41 PM: v16.19.1 is already installed.
4:50:41 PM: Now using node v16.19.1 (npm v8.19.3)
4:50:41 PM: Enabling Node.js Corepack
4:50:41 PM: Started restoring cached build plugins
4:50:41 PM: Finished restoring cached build plugins
4:50:41 PM: Started restoring cached corepack dependencies
4:50:41 PM: Finished restoring cached corepack dependencies
4:50:41 PM: No npm workspaces detected
4:50:41 PM: Started restoring cached node modules
4:50:41 PM: Finished restoring cached node modules
4:50:41 PM: Installing npm packages using npm version 8.19.3
4:51:01 PM: npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
4:51:01 PM: npm WARN deprecated rollup-plugin-terser@7.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-terser
4:51:02 PM: npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: Array.prototype.sort() - JavaScript | MDN
4:51:06 PM: npm WARN deprecated w3c-hr-time@1.0.2: Use your platform’s native performance.now() and performance.timeOrigin.npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
4:51:09 PM: added 1413 packages, and audited 1414 packages in 27s
4:51:09 PM: 233 packages are looking for funding
4:51:09 PM: run npm fund for details
4:51:09 PM: 6 high severity vulnerabilities
4:51:09 PM: To address all issues (including breaking changes), run:
4:51:09 PM: npm audit fix --force
4:51:09 PM: Run npm audit for details.
4:51:09 PM: npm packages installed
4:51:09 PM: Started restoring cached go cache
4:51:09 PM: Finished restoring cached go cache
4:51:09 PM: Installing Go version 1.19.5 (requested 1.19.5)
4:51:14 PM: go version go1.19.5 linux/amd64
4:51:15 PM: Python version set to 3.8
4:51:15 PM: Attempting Ruby version 2.7.2, read from environment
4:51:16 PM: Using Ruby version 2.7.2
4:51:17 PM: Using PHP version 8.0
4:51:17 PM: Install dependencies script success
4:51:17 PM: Starting build script
4:51:17 PM: Using stage install dependencies
4:51:18 PM: Detected 1 framework(s)
4:51:18 PM: β€œcreate-react-app” at version β€œ5.0.1”
4:51:18 PM: Section completed: initializing
4:51:19 PM: ​
4:51:19 PM: Netlify Build
4:51:19 PM: ────────────────────────────────────────────────────────────────
4:51:19 PM: ​
4:51:19 PM: ❯ Version
4:51:19 PM: @netlify/build 29.8.0
4:51:19 PM: ​
4:51:19 PM: ❯ Flags
4:51:19 PM: baseRelDir: true
4:51:19 PM: buildId: 6423531a6d3b9d25ea9ed2c8
4:51:19 PM: deployId: 6423531a6d3b9d25ea9ed2ca
4:51:19 PM: ​
4:51:19 PM: ❯ Current directory
4:51:19 PM: /opt/build/repo
4:51:19 PM: ​
4:51:19 PM: ❯ Config file
4:51:19 PM: No config file was defined: using default values.
4:51:19 PM: ​
4:51:19 PM: ❯ Context
4:51:19 PM: production
4:51:19 PM: ​
4:51:19 PM: 1. Build command from Netlify app
4:51:19 PM: ────────────────────────────────────────────────────────────────
4:51:19 PM: ​
4:51:19 PM: $ npm run build
4:51:19 PM: > furry-friends-test@0.1.0 build
4:51:19 PM: > react-scripts build
4:51:20 PM: Could not find a required file.
4:51:20 PM: Name: index.html
4:51:20 PM: Searched in: /opt/build/repo/public
4:51:20 PM: ​
4:51:20 PM: β€œbuild.command” failed
4:51:20 PM: ────────────────────────────────────────────────────────────────
4:51:20 PM: ​
4:51:20 PM: Error message
4:51:20 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
4:51:20 PM: ​
4:51:20 PM: Error location
4:51:20 PM: In Build command from Netlify app:
4:51:20 PM: npm run build
4:51:20 PM: ​
4:51:20 PM: Resolved config
4:51:20 PM: build:
4:51:20 PM: command: npm run build
4:51:20 PM: commandOrigin: ui
4:51:20 PM: environment:
4:51:20 PM: - REACT_APP_DOG_API_KEY
4:51:20 PM: - REACT_APP_DOG_API_URL
4:51:20 PM: publish: /opt/build/repo/build
4:51:20 PM: publishOrigin: ui
4:51:21 PM: Caching artifacts
4:51:21 PM: Started saving node modules
4:51:21 PM: Finished saving node modules
4:51:21 PM: Started saving build plugins
4:51:21 PM: Failed during stage β€˜building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
4:51:21 PM: Finished saving build plugins
4:51:21 PM: Started saving corepack cache
4:51:21 PM: Finished saving corepack cache
4:51:21 PM: Started saving pip cache
4:51:21 PM: Finished saving pip cache
4:51:21 PM: Started saving emacs cask dependencies
4:51:21 PM: Finished saving emacs cask dependencies
4:51:21 PM: Started saving maven dependencies
4:51:21 PM: Finished saving maven dependencies
4:51:21 PM: Started saving boot dependencies
4:51:21 PM: Finished saving boot dependencies
4:51:21 PM: Started saving rust rustup cache
4:51:21 PM: Finished saving rust rustup cache
4:51:21 PM: Started saving go dependencies
4:51:21 PM: Finished saving go dependencies
4:51:21 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:51:21 PM: Failing build: Failed to build site
4:51:22 PM: Finished processing build request in 43.646s

The new git repository

I’m just lost at this point

Again, the git repository does not contains all the project files, only the build directory which is the result of a local build you have done. Therefore as previously stated there is nothing for Netlify to build, thus the build command npm run build you have set is throwing an error.

As stated previously, if you wish to have Netlify build the project, you need to commit and push all relevant project files to GitHubβ€”in the case of React a public directory and src directory at the minimum.

Alternatively you can remove the build command npm run build from Netlify, set the publish directory to build and Netlify will deploy the project you have built locally and pushed to git.

Thank you!! I got it to work!

Hi, thanks for coming back and confirming you found your solution!