Failed during stage "building site": Build script returned non-zero exit code: 2

Hi,
I’ve checked all prev. posts on the subject.
Nothing is helping :frowning:

When i build locally, there is no errors.
but when trying to deploy on netlify having this error :

4:35:28 PM: Netlify Build                                                 
4:35:28 PM: ────────────────────────────────────────────────────────────────
4:35:28 PM: ​
4:35:28 PM: ❯ Version
4:35:28 PM:   @netlify/build 29.33.2
4:35:28 PM: ​
4:35:28 PM: ❯ Flags
4:35:28 PM:   baseRelDir: true
4:35:28 PM:   buildId: 65bfa0969ae48668782a5e71
4:35:28 PM:   deployId: 65bfa0969ae48668782a5e73
4:35:28 PM: ​
4:35:28 PM: ❯ Current directory
4:35:28 PM:   /opt/build/repo
4:35:28 PM: ​
4:35:28 PM: ❯ Config file
4:35:28 PM:   No config file was defined: using default values.
4:35:28 PM: ​
4:35:28 PM: ❯ Context
4:35:28 PM:   production
4:35:28 PM: ​
4:35:28 PM: Build command from Netlify app                                
4:35:28 PM: ────────────────────────────────────────────────────────────────
4:35:28 PM: ​
4:35:28 PM: $ CI= npm run build
4:35:28 PM: > coinindex@0.1.0 build
4:35:28 PM: > react-scripts build
4:35:29 PM: Creating an optimized production build...
4:35:35 PM: Failed to compile.
4:35:35 PM: 
4:35:35 PM: Module not found: Error: Can"t resolve "./HalvingCounter" in "/opt/build/repo/src/components/Homepage"
4:35:35 PM: ​
4:35:35 PM: "build.command" failed                                        
4:35:35 PM: ────────────────────────────────────────────────────────────────
4:35:35 PM: ​
4:35:35 PM:   Error message
4:35:35 PM:   Command failed with exit code 1: CI= npm run build (https://ntl.fyi/exit-code-1)
4:35:35 PM: ​
4:35:35 PM:   Error location
4:35:35 PM:   In Build command from Netlify app:
4:35:35 PM:   CI= npm run build
4:35:35 PM: ​
4:35:35 PM:   Resolved config
4:35:35 PM:   build:
4:35:35 PM:     command: CI= npm run build
4:35:35 PM:     commandOrigin: ui
4:35:35 PM:     environment:
4:35:35 PM:       - CI
4:35:35 PM:       - REACT_APP_CMS_API_KEY
4:35:35 PM:       - REACT_APP_NEWSDATA_API_KEY
4:35:35 PM:       - REACT_APP_YOUTUBE_API_KEY
4:35:35 PM:     publish: /opt/build/repo/build
4:35:35 PM:     publishOrigin: ui
4:35:35 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:35:35 PM: Failing build: Failed to build site
4:35:35 PM: Finished processing build request in 32.652s
4:35:35 PM: Failed during stage "building site": Build script returned non-zero exit code: 2

my packge.json

{
  "name": "coinindex",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "apexcharts": "^3.37.1",
    "axios": "^1.2.5",
    "bootstrap": "^5.2.3",
    "bootstrap-icons": "^1.10.3",
    "react": "^18.2.0",
    "react-apexcharts": "^1.4.0",
    "react-bootstrap": "^2.7.0",
    "react-bootstrap-icons": "^1.10.2",
    "react-dom": "^18.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "5.0.1",
    "react-youtube": "^10.1.0",
    "styled-components": "^6.0.8",
    "universal-cookie": "^4.0.4",
    "web-vitals": "^2.1.4"
  },
  "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"
    ]
  }
}

@nickglu86 The error isn’t about the exit codes, that’s just the end result.

That’s not really necessary.

While the build failed because Netlify received an exit code that wasn’t β€˜success’, many things can throw the same exit codes. You need to look higher in your build log to determine the cause of the error.

In your case it is:

4:35:35 PM: Module not found: Error: Can"t resolve "./HalvingCounter" in "/opt/build/repo/src/components/Homepage"

That says that in your /src/components/Homepage file, that you’re trying to import ./HalvingCounter and that it couldn’t find it.

You should make sure the file is in that location, or adjust the reference to where it actually is, and you should ensure the error isn’t being caused by case-sensitivity (Netlify’s build enviroment is case-sensitive).

1 Like