Build command failed on React app

PLEASE help us help you by writing a good post!

  • we need to know your netlify site name. Example: cheery-dasik-2ee03b.netlify.app
  • DNS issues? Tell us the custom domain, tell us the error message! We can’t help if we don’t know your domain.
  • Build problems? Link or paste the FULL build log & build settings screenshot

The better the post - the faster the answer.

This is the Error code :

10:11:34 PM: “build.command” failed
10:11:34 PM: ────────────────────────────────────────────────────────────────
10:11:34 PM: ​
10:11:34 PM: Error message
10:11:34 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
10:11:34 PM: ​
10:11:34 PM: Error location
10:11:34 PM: In Build command from Netlify app:
10:11:34 PM: npm run build
10:11:34 PM: ​
10:11:34 PM: Resolved config
10:11:34 PM: build:
10:11:34 PM: command: npm run build
10:11:34 PM: commandOrigin: ui
10:11:34 PM: publish: /opt/build/repo/build
10:11:34 PM: publishOrigin: ui
10:11:35 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2
10:11:35 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
10:11:35 PM: Failing build: Failed to build site
10:11:35 PM: Finished processing build request in 46.834s

some one please share the solution.

I have changed build to

“build”: “CI= npm run build”,

This was the result

10:34:15 PM: > CI= npm run build

10:34:15 PM: > webapp@0.2.0 build

10:34:15 PM: > CI= npm run build

10:34:15 PM: > webapp@0.2.0 build

10:34:15 PM: > CI= npm run build

10:34:16 PM: > webapp@0.2.0 build

10:34:16 PM: > CI= npm run build

10:34:16 PM: > webapp@0.2.0 build

10:34:16 PM: > CI= npm run build

10:34:17 PM: > webapp@0.2.0 build

10:34:17 PM: > CI= npm run build

10:34:17 PM: > webapp@0.2.0 build

10:34:17 PM: > CI= npm run build

10:34:17 PM: > webapp@0.2.0 build

10:34:17 PM: > CI= npm run build

10:34:18 PM: > webapp@0.2.0 build

10:34:18 PM: > CI= npm run build

10:34:18 PM: > webapp@0.2.0 build

10:34:18 PM: > CI= npm run build

10:34:18 PM: > webapp@0.2.0 build

10:34:18 PM: > CI= npm run build

10:34:19 PM: > webapp@0.2.0 build

10:34:19 PM: > CI= npm run build

10:34:19 PM: npm ERR! code ENOENT

10:34:19 PM: npm ERR! syscall open

10:34:19 PM: npm ERR! path /opt/build/repo/package.json

10:34:19 PM: npm ERR! errno -2

10:34:19 PM: npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open "/opt/build/repo/package.json"

10:34:19 PM: npm ERR! enoent This is related to npm not being able to find a file.

10:34:19 PM: npm ERR! enoent

10:34:19 PM: npm ERR! A complete log of this run can be found in: /opt/buildhome/.npm/_logs/2023-10-30T17_04_19_474Z-debug-0.log

10:34:22 PM: Execution cancelled

10:34:22 PM: Error running command: Command was cancelled

10:34:22 PM: Failing build: Failed to build site

10:34:22 PM: Finished processing build request in 1m34.706s

I have changed the files to
netlify.toml

and


[build]
  command = "npm run build && node ./sayHello.js"

This was the result


10:56:26 PM: "build.command" failed                                        
10:56:26 PM: ────────────────────────────────────────────────────────────────
10:56:26 PM: ​
10:56:26 PM:   Error message
10:56:26 PM:   Command failed with exit code 1: npm run build && node ./sayHello.js (https://ntl.fyi/exit-code-1)
10:56:26 PM: ​
10:56:26 PM:   Error location
10:56:26 PM:   In build.command from netlify.toml:
10:56:26 PM:   npm run build && node ./sayHello.js
10:56:26 PM: ​
10:56:26 PM:   Resolved config
10:56:26 PM:   build:
10:56:26 PM:     command: npm run build && node ./sayHello.js
10:56:26 PM:     commandOrigin: config
10:56:26 PM:     publish: /opt/build/repo/build
10:56:26 PM:     publishOrigin: ui
10:56:26 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
10:56:26 PM: Failing build: Failed to build site
10:56:26 PM: Finished processing build request in 47.623s
10:56:26 PM: Failed during stage "building site": Build script returned non-zero exit code: 2

@70ny npm run build runs the build script in your package.json, what is in your package.json?

Here is the git file

@70ny It looks like you’ve been duplicating the command that should be your Netlify Build command (either in the settings or the netlify.toml as you’ve shown), into your package.json build script.

You don’t want to do this, it’ll cause an infinite loop as per one of the logs supplied above.

It would look like this:

  1. The initial execution from the netlify.toml command runs npm run build
  2. npm run build executes the build script in the package.json which runs npm run build
  3. npm run build executes the build script in the package.json which runs npm run build
  4. npm run build executes the build script in the package.json which runs npm run build
    … etc

Based on your other package.json scripts executing react-scripts you almost certainly want to change the build one back to what it would have been originally:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",

That way when npm run build is executed via the netlify.toml it runs react-scripts build.