Build failed due to a user error: Build script returned non-zero exit code: 2

Hi, I’m facing an issue while using continuous deployment to deploy my react app.
This is the error i am getting - Build failed due to a user error: Build script returned non-zero exit code: 2.
However, when i create a build file in IDE using npm run build there are no issues and i am able to deploy the app using manual deployment.

Could someone please help me on where i am going wrong.
1:50:36 PM: build-image version: 9495be345de05eb8e91837e5d92c5afd0dbd65f6 (focal)
1:50:36 PM: buildbot version: 9495be345de05eb8e91837e5d92c5afd0dbd65f6
1:50:36 PM: Fetching cached dependencies
1:50:36 PM: Failed to fetch cache, continuing with build
1:50:36 PM: Starting to prepare the repo for build
1:50:37 PM: No cached dependencies found. Cloning fresh repo
1:50:37 PM: git clone --filter=blob:none https://github.com/akshataxx/React-Website
1:50:37 PM: Preparing Git Reference refs/heads/main
1:50:38 PM: Parsing package.json dependencies
1:50:39 PM: Starting build script
1:50:39 PM: Installing dependencies
1:50:39 PM: Python version set to 2.7
1:50:39 PM: v16.19.0 is already installed.
1:50:39 PM: Now using node v16.19.0 (npm v8.19.3)
1:50:40 PM: Enabling Node.js Corepack
1:50:40 PM: Started restoring cached build plugins
1:50:40 PM: Finished restoring cached build plugins
1:50:40 PM: Attempting Ruby version 2.7.2, read from environment
1:50:40 PM: Using Ruby version 2.7.2
1:50:40 PM: Using PHP version 8.0
1:50:40 PM: Started restoring cached go cache
1:50:40 PM: Finished restoring cached go cache
1:50:40 PM: go version go1.19.5 linux/amd64
1:50:41 PM: Detected 0 framework(s)
1:50:41 PM: Installing missing commands
1:50:41 PM: Verify run directory
1:50:41 PM: Section completed: initializing
1:50:42 PM: ​
1:50:42 PM: Netlify Build
1:50:42 PM: ────────────────────────────────────────────────────────────────
1:50:42 PM: ​
1:50:42 PM: ❯ Version
1:50:42 PM: @netlify/build 29.5.4
1:50:42 PM: ​
1:50:42 PM: ❯ Flags
1:50:42 PM: baseRelDir: true
1:50:42 PM: buildId: 63e853f9cc473a2a33202c42
1:50:42 PM: deployId: 63e853f9cc473a2a33202c44
1:50:42 PM: ​
1:50:42 PM: ❯ Current directory
1:50:42 PM: /opt/build/repo
1:50:42 PM: ​
1:50:42 PM: ❯ Config file
1:50:42 PM: No config file was defined: using default values.
1:50:42 PM: ​
1:50:42 PM: ❯ Context
1:50:42 PM: production
1:50:42 PM: ​
1:50:42 PM: 1. Build command from Netlify app
1:50:42 PM: ────────────────────────────────────────────────────────────────
1:50:42 PM: ​
1:50:42 PM: $ CI= npm run build
1:50:43 PM: npm ERR! code ENOENT
1:50:43 PM: npm ERR! syscall open
1:50:43 PM: npm ERR! path /opt/build/repo/package.json
1:50:43 PM: npm ERR! errno -2
1:50:43 PM: npm ERR! enoent ENOENT: no such file or directory, open ‘/opt/build/repo/package.json’
1:50:43 PM: npm ERR! enoent This is related to npm not being able to find a file.
1:50:43 PM: npm ERR! enoent
1:50:43 PM: npm ERR! A complete log of this run can be found in:
1:50:43 PM: npm ERR! /opt/buildhome/.npm/_logs/2023-02-12T02_50_42_977Z-debug-0.log
1:50:43 PM: ​
1:50:43 PM: “build.command” failed
1:50:43 PM: ────────────────────────────────────────────────────────────────
1:50:44 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2
1:50:43 PM: ​
1:50:43 PM: Error message
1:50:43 PM: Command failed with exit code 254: CI= npm run build
1:50:43 PM: ​
1:50:43 PM: Error location
1:50:43 PM: In Build command from Netlify app:
1:50:43 PM: CI= npm run build
1:50:43 PM: ​
1:50:43 PM: Resolved config
1:50:43 PM: build:
1:50:43 PM: command: CI= npm run build
1:50:43 PM: commandOrigin: ui
1:50:43 PM: environment:
1:50:43 PM: - CI
1:50:43 PM: publish: /opt/build/repo/build
1:50:43 PM: publishOrigin: ui
1:50:43 PM: Caching artifacts
1:50:43 PM: Started saving build plugins
1:50:43 PM: Finished saving build plugins
1:50:43 PM: Started saving pip cache
1:50:43 PM: Finished saving pip cache
1:50:43 PM: Started saving emacs cask dependencies
1:50:43 PM: Finished saving emacs cask dependencies
1:50:43 PM: Started saving maven dependencies
1:50:43 PM: Finished saving maven dependencies
1:50:43 PM: Started saving boot dependencies
1:50:43 PM: Finished saving boot dependencies
1:50:43 PM: Started saving rust rustup cache
1:50:43 PM: Finished saving rust rustup cache
1:50:43 PM: Started saving go dependencies
1:50:43 PM: Finished saving go dependencies
1:50:44 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
1:50:44 PM: Failing build: Failed to build site
1:50:44 PM: Finished processing build request in 7.669s

@akshataxx

Your build command is:

1:50:42 PM: $ CI= npm run build

The error is:

1:50:43 PM: npm ERR! enoent ENOENT: no such file or directory, open ‘/opt/build/repo/package.json’
1:50:43 PM: npm ERR! enoent This is related to npm not being able to find a file.

Do you have your package.json commited to your repository?

(I couldn’t check your repository to confirm as it’s set to private).

Hi Nathan I have made my repo public now and yes i have committed my package.json file to the repo.

Should i be adding $CI = npm run build as the build command

@akshataxx Your project appears to be in the react-portfolio sub-folder, so you will need to set that as your Base directory in the Build settings.

There’s no need to change the Build command, the issue will be as the error states, when npm run build is occuring, npm cannot find your package.json, and that will be because it’s executing in the root of your project, and there is no package.json there.

Thanks so much!! That worked :slight_smile: