Code works locally, but build.command failed (build script returned non-zero exit code: 2)

Hi, this is the first time I try to push something on github and deploy here, I’m a total beginner, so I have no idea what went wrong.
This is the site name: https://grand-sunshine-d97e87.netlify.app/
It deploys from this repo in github: GitHub - NoNameOverlord/random-quote-machine: Random Quote Machine for a FreeCodeCamp project, functional but not styled
When I work locally on Visual Studio, the code works and it shows the page as it should be with all its functionalities, but I can’t seem .
I have already tried to change “npm run build” to “CI= npm run build” to no avail.

Here the full building log:

12:09:05 PM: build-image version: 2821c5fd78f4042b4bb03d028b1983667d2e2d9c (focal)
12:09:05 PM: buildbot version: 9651a880ad155a86e0118402a03ac3a5e674b41e
12:09:05 PM: Building without cache
12:09:05 PM: Starting to prepare the repo for build
12:09:05 PM: No cached dependencies found. Cloning fresh repo
12:09:05 PM: git clone --filter=blob:none GitHub - NoNameOverlord/random-quote-machine: Random Quote Machine for a FreeCodeCamp project, functional but not styled
12:09:05 PM: Preparing Git Reference refs/heads/main
12:09:06 PM: Starting to install dependencies
12:09:06 PM: Python version set to 3.8
12:09:07 PM: Attempting Ruby version 2.7.2, read from environment
12:09:07 PM: Using Ruby version 2.7.2
12:09:08 PM: Started restoring cached go cache
12:09:08 PM: Finished restoring cached go cache
12:09:08 PM: go version go1.19.13 linux/amd64
12:09:08 PM: Using PHP version 8.0
12:09:09 PM: Downloading and installing node v20.11.0…
12:09:09 PM: Downloading https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.xz
12:09:10 PM: Computing checksum with sha256sum
12:09:10 PM: Checksums matched!
12:09:12 PM: Now using node v20.11.0 (npm v10.2.4)
12:09:12 PM: Enabling Node.js Corepack
12:09:12 PM: Started restoring cached build plugins
12:09:12 PM: Finished restoring cached build plugins
12:09:12 PM: Started restoring cached corepack dependencies
12:09:12 PM: Finished restoring cached corepack dependencies
12:09:12 PM: No npm workspaces detected
12:09:12 PM: Started restoring cached node modules
12:09:12 PM: Finished restoring cached node modules
12:09:12 PM: Installing npm packages using npm version 10.2.4
12:09:15 PM: added 219 packages, and audited 220 packages in 2s
12:09:15 PM: 41 packages are looking for funding
12:09:15 PM: run npm fund for details
12:09:15 PM: found 0 vulnerabilities
12:09:15 PM: npm packages installed
12:09:15 PM: Successfully installed dependencies
12:09:15 PM: Starting build script
12:09:16 PM: Detected 1 framework(s)
12:09:16 PM: “vite” at version “5.0.11”
12:09:16 PM: Section completed: initializing
12:09:18 PM: ​
12:09:18 PM: Netlify Build
12:09:18 PM: ────────────────────────────────────────────────────────────────
12:09:18 PM: ​
12:09:18 PM: ❯ Version
12:09:18 PM: @netlify/build 29.32.0
12:09:18 PM: ​
12:09:18 PM: ❯ Flags
12:09:18 PM: baseRelDir: true
12:09:18 PM: buildId: 65a11dcfbbae71b5de56e591
12:09:18 PM: deployId: 65a11dcfbbae71b5de56e593
12:09:18 PM: ​
12:09:18 PM: ❯ Current directory
12:09:18 PM: /opt/build/repo
12:09:18 PM: ​
12:09:18 PM: ❯ Config file
12:09:18 PM: No config file was defined: using default values.
12:09:18 PM: ​
12:09:18 PM: ❯ Context
12:09:18 PM: production
12:09:18 PM: ​
12:09:18 PM: Build command from Netlify app
12:09:18 PM: ────────────────────────────────────────────────────────────────
12:09:18 PM: ​
12:09:18 PM: $ CI= npm run build
12:09:18 PM: > random-quote-machine@0.0.0 build
12:09:18 PM: > tsc && vite build
12:09:19 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2
12:09:19 PM: src/App.tsx(22,39): error TS2339: Property “quote” does not exist on type “never”.
12:09:19 PM: src/App.tsx(23,40): error TS2339: Property “author” does not exist on type “never”.
12:09:19 PM: src/App.tsx(35,38): error TS2339: Property “quote” does not exist on type “never”.
12:09:19 PM: src/App.tsx(36,39): error TS2339: Property “author” does not exist on type “never”.
12:09:19 PM: ​
12:09:19 PM: “build.command” failed
12:09:19 PM: ────────────────────────────────────────────────────────────────
12:09:19 PM: ​
12:09:19 PM: Error message
12:09:19 PM: Command failed with exit code 2: CI= npm run build (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
12:09:19 PM: ​
12:09:19 PM: Error location
12:09:19 PM: In Build command from Netlify app:
12:09:19 PM: CI= npm run build
12:09:19 PM: ​
12:09:19 PM: Resolved config
12:09:19 PM: build:
12:09:19 PM: command: CI= npm run build
12:09:19 PM: commandOrigin: ui
12:09:19 PM: publish: /opt/build/repo/build
12:09:19 PM: publishOrigin: ui
12:09:19 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:09:19 PM: Failing build: Failed to build site
12:09:19 PM: Finished processing build request in 14.898s

I tried to drop the project manually here on Netlify: https://resilient-queijadas-a33cb9.netlify.app/
Yet, while I can see the files are there, the website shows just a blank page and none of its functionalities.

@francesco.barzetta The error is shown in the log:

When you say:

This will be because you’re running something different locally than on Netlify.
You’re likely running only npm run dev, which executes vite.

You should try running exactly the same thing locally that you’re running on Netlify.
Try running npm run build, which executes tsc && vite build.

If you do that you’ll see the same errors:

They are being thrown by tsc.

If you remove it from the “build” script in your package.json you will find that the build occurs, but I cannot advise if it’s necessary for your project, so you should familiarize yourself with what you’re working with and fix any errors.

1 Like

I was indeed running npm run dev.
That clears up everything. I’ll try to make my code cleaner, thank you very much!

EDIT: Yes, I was silly because I thought that it was a warning and not an error. Fixed it super quickly and now everything works!