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

  *******  I have a problem while deploying on netlify. Please help..  *******

8:24:08 PM: 3. @netlify/plugin-nextjs (onBuild event)
8:24:08 PM: ────────────────────────────────────────────────────────────────
8:24:08 PM: ​
8:24:08 PM: ​
8:24:08 PM: ────────────────────────────────────────────────────────────────
8:24:08 PM: Plugin “@netlify/plugin-nextjs” failed
8:24:09 PM: Creating deploy upload records
8:24:08 PM: ────────────────────────────────────────────────────────────────
8:24:08 PM: ​
8:24:08 PM: Error message
8:24:08 PM: Error: The directory “/opt/build/repo/.next” does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
8:24:08 PM: In most cases it should be set to “.next”, unless you have chosen a custom “distDir” in your Next config.
8:24:10 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)
8:24:08 PM: If you are using “next export” then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to “true”.
8:24:08 PM: ​
8:24:08 PM: Plugin details
8:24:08 PM: Package: @netlify/plugin-nextjs
8:24:08 PM: Version: 4.15.0
8:24:08 PM: Repository: git+https://github.com/netlify/netlify-plugin-nextjs.git
8:24:08 PM: npm link: https://www.npmjs.com/package/@netlify/plugin-nextjs
8:24:08 PM: Report issues: Issues · netlify/next-runtime · GitHub
8:24:08 PM: ​
8:24:08 PM: Error location
8:24:08 PM: In “onBuild” event in “@netlify/plugin-nextjs” from Netlify app and package.json
8:24:08 PM: at checkNextSiteHasBuilt (/node_modules/@netlify/plugin-nextjs/lib/helpers/verification.js:82:16)
8:24:08 PM: at onBuild (/node_modules/@netlify/plugin-nextjs/lib/index.js:42:50)
8:24:08 PM: ​
8:24:08 PM: Resolved config
8:24:08 PM: build:
8:24:08 PM: command: npm run
8:24:08 PM: commandOrigin: ui
8:24:08 PM: environment:
8:24:08 PM: - NEXT_PRIVATE_TARGET
8:24:08 PM: publish: /opt/build/repo/.next
8:24:08 PM: publishOrigin: ui
8:24:08 PM: plugins:
8:24:08 PM: - inputs: {}
8:24:08 PM: origin: ui
8:24:08 PM: package: ‘@netlify/plugin-nextjs’
8:24:08 PM: Caching artifacts
8:24:08 PM: Started saving node modules
8:24:08 PM: Finished saving node modules
8:24:08 PM: Started saving build plugins
8:24:09 PM: Finished saving build plugins
8:24:09 PM: Started saving pip cache
8:24:09 PM: Finished saving pip cache
8:24:09 PM: Started saving emacs cask dependencies
8:24:09 PM: Finished saving emacs cask dependencies
8:24:09 PM: Started saving maven dependencies
8:24:09 PM: Finished saving maven dependencies
8:24:09 PM: Started saving boot dependencies
8:24:09 PM: Finished saving boot dependencies
8:24:09 PM: Started saving rust rustup cache
8:24:09 PM: Finished saving rust rustup cache
8:24:09 PM: Started saving go dependencies
8:24:09 PM: Finished saving go dependencies
8:24:09 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
8:24:10 PM: Failing build: Failed to build site
8:24:10 PM: Finished processing build request in 31.713480941s

The problem has been explained in these lines. If you think that’s incorrect, please share your site name.

this is my site name ‘dawwas-portfolio’.

The problem is, you’re running npm run as your build command: Netlify App. This is incorrect.

Change it here: Netlify App to npm run build.

I was using ‘npm run build’, but this is what happened

8:56:41 PM: 2. Build command from Netlify app
8:56:41 PM: ────────────────────────────────────────────────────────────────
8:56:41 PM: ​
8:56:41 PM: $ npm run build
8:56:41 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special
8:56:41 PM: npm WARN config location in the cache, and they are managed by
8:56:41 PM: npm WARN config cacache.
8:56:41 PM: > craftzdog-homepage@1.0.0 build
8:56:41 PM: > next build
8:56:42 PM: info - SWC minify release candidate enabled. swc-minify-enabled | Next.js
8:56:42 PM: info - Linting and checking validity of types…
8:56:44 PM:
8:56:44 PM: Failed to compile.
8:56:44 PM: ./pages/index.js
8:56:44 PM: 42:24 Error: ' can be escaped with ', ‘, ', ’. react/no-unescaped-entities
8:56:44 PM: ./pages/works.js
8:56:44 PM: 1:42 Error: ‘Divider’ is defined but never used. Allowed unused vars must match /^_/u. no-unused-vars
8:56:44 PM: info - Need to disable some ESLint rules? Learn more here: Basic Features: ESLint | Next.js
8:56:44 PM: ​
8:56:44 PM: ────────────────────────────────────────────────────────────────
8:56:44 PM: “build.command” failed
8:56:44 PM: ────────────────────────────────────────────────────────────────
8:56:44 PM: ​
8:56:44 PM: Error message
8:56:44 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
8:56:44 PM: ​
8:56:44 PM: Error location
8:56:44 PM: In Build command from Netlify app:
8:56:44 PM: npm run build
8:56:44 PM: ​
8:56:44 PM: Resolved config
8:56:44 PM: build:
8:56:44 PM: command: npm run build
8:56:44 PM: commandOrigin: ui
8:56:44 PM: environment:
8:56:44 PM: - NEXT_PRIVATE_TARGET
8:56:44 PM: publish: /opt/build/repo/.next
8:56:44 PM: publishOrigin: ui
8:56:44 PM: plugins:
8:56:44 PM: - inputs: {}
8:56:44 PM: origin: ui
8:56:44 PM: package: ‘@netlify/plugin-nextjs’
8:56:44 PM: Caching artifacts
8:56:45 PM: Creating deploy upload records
8:56:44 PM: Started saving node modules
8:56:44 PM: Finished saving node modules
8:56:44 PM: Started saving build plugins
8:56:44 PM: Finished saving build plugins
8:56:44 PM: Started saving pip cache
8:56:45 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)
8:56:44 PM: Finished saving pip cache
8:56:44 PM: Started saving emacs cask dependencies
8:56:44 PM: Finished saving emacs cask dependencies
8:56:44 PM: Started saving maven dependencies
8:56:44 PM: Finished saving maven dependencies
8:56:44 PM: Started saving boot dependencies
8:56:44 PM: Finished saving boot dependencies
8:56:44 PM: Started saving rust rustup cache
8:56:44 PM: Finished saving rust rustup cache
8:56:44 PM: Started saving go dependencies
8:56:44 PM: Finished saving go dependencies
8:56:45 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
8:56:45 PM: Failing build: Failed to build site
8:56:46 PM: Finished processing build request in 41.492668148s

The problems are here and

If you want to ignore those warnings, try going to on the link in the logs:


But there i’m using &apos(in line 36). Then, there’s no ‘Divider’ in my code.

Well, it must exist somewhere for Next.js to complain about it. But as I said, you can skip refactoring this, but disabling the ESLint rules:

I try this

But, still have problem
11:11:50 PM: 2. Build command from Netlify app
11:11:50 PM: ────────────────────────────────────────────────────────────────
11:11:50 PM: ​
11:11:50 PM: $ npm run build
11:11:50 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special
11:11:50 PM: npm WARN config location in the cache, and they are managed by
11:11:50 PM: npm WARN config cacache.
11:11:50 PM: > craftzdog-homepage@1.0.0 build
11:11:50 PM: > next build
11:11:50 PM: info - SWC minify release candidate enabled. swc-minify-enabled | Next.js
11:11:50 PM: info - Linting and checking validity of types…
11:11:52 PM:
11:11:52 PM: Failed to compile.
11:11:52 PM: ./pages/index.js
11:11:52 PM: 42:24 Error: ' can be escaped with ', ‘, ', ’. react/no-unescaped-entities
11:11:52 PM: ./pages/works.js
11:11:52 PM: 1:42 Error: ‘Divider’ is defined but never used. Allowed unused vars must match /^_/u. no-unused-vars
11:11:52 PM: info - Need to disable some ESLint rules? Learn more here: Basic Features: ESLint | Next.js
11:11:53 PM: ​
11:11:53 PM: ────────────────────────────────────────────────────────────────
11:11:53 PM: “build.command” failed
11:11:53 PM: ────────────────────────────────────────────────────────────────
11:11:53 PM: ​
11:11:53 PM: Error message
11:11:53 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
11:11:53 PM: ​
11:11:53 PM: Error location
11:11:53 PM: In Build command from Netlify app:
11:11:53 PM: npm run build
11:11:53 PM: ​
11:11:53 PM: Resolved config
11:11:53 PM: build:
11:11:53 PM: command: npm run build
11:11:53 PM: commandOrigin: ui
11:11:53 PM: environment:
11:11:53 PM: - NEXT_PRIVATE_TARGET
11:11:53 PM: publish: /opt/build/repo/.next
11:11:53 PM: publishOrigin: ui
11:11:53 PM: plugins:
11:11:53 PM: - inputs: {}
11:11:53 PM: origin: ui
11:11:54 PM: Creating deploy upload records
11:11:53 PM: package: ‘@netlify/plugin-nextjs’
11:11:53 PM: Caching artifacts
11:11:53 PM: Started saving node modules
11:11:53 PM: Finished saving node modules
11:11:54 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:11:53 PM: Started saving build plugins
11:11:53 PM: Finished saving build plugins
11:11:53 PM: Started saving pip cache
11:11:53 PM: Finished saving pip cache
11:11:53 PM: Started saving emacs cask dependencies
11:11:53 PM: Finished saving emacs cask dependencies
11:11:53 PM: Started saving maven dependencies
11:11:53 PM: Finished saving maven dependencies
11:11:53 PM: Started saving boot dependencies
11:11:53 PM: Finished saving boot dependencies
11:11:53 PM: Started saving rust rustup cache
11:11:53 PM: Finished saving rust rustup cache
11:11:53 PM: Started saving go dependencies
11:11:53 PM: Finished saving go dependencies
11:11:54 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:11:54 PM: Failing build: Failed to build site
11:11:54 PM: Finished processing build request in 36.481528496s

The docs provide an example, you need to disable rules for your particular site: