跳到正文
博客

Netlify "Build script returned non-zero exit code: 2" fix

Netlify build fails with "Build script returned non-zero exit code: 2" but works locally? Find the real error in the log and fix case, package or Node issues.

The Liite team阅读约 9 分钟

Short answer

The exit code line is only a summary. Scroll up in the deploy log to the first real error, which names the file, module or step that failed. Then check the usual local-versus-Netlify differences: file name case, files that were never committed, dependencies, the Node version and environment variables.

Your site builds fine on your machine, then Netlify stops with this:

Build script returned non-zero exit code: 2

Sometimes ENOENT: no such file or directory appears a few lines above it. The exit code is not the problem. It is the last line of a log whose real cause sits higher up.

Why it happens

Netlify's own error reference says a non-zero exit code means your build failed, which means your deploy failed. It also tells you to scroll in your build log to find the failure before the non-zero exit code. The number itself (2, 1, 127) is whatever status your build command or one of its tools returned, so it rarely helps to search for it.

Your build works locally and fails on Netlify because the two environments differ. The most common differences, roughly in order of how often they cause this:

  • File name case. Netlify builds on Linux, where Logo.png and logo.png are different files. macOS and Windows file systems usually ignore case, so an import with the wrong capitalization works for you and fails on the server with ENOENT or "Module not found". Git on those systems can also miss a rename that only changes case.
  • Files that exist only on your disk. A file that is untracked or covered by .gitignore is not in the clone Netlify builds from.
  • Dependencies that differ. Your local node_modules may contain packages or versions that are not in package.json or the lockfile. A clean install on Netlify gets something else.
  • A different Node version. Netlify's current build image is based on Ubuntu version 24.04, and the default Node.js version is 24 as of the Netlify docs checked in November 2026. If you build locally on an older or newer Node, packages can behave differently.
  • Environment variables. Variables you set only on your machine are missing in the build. Netlify's docs note that the build system does not read .env files.
  • Wrong build settings. A wrong build command, publish directory or base directory in a monorepo can look like a missing file.

I can't tell which of these applies to you from the exit code alone. The log decides.

Step-by-step fixes

1. Find the real error in the log

  1. In Netlify, go to your project and select Deploys.
  2. Choose the deploy that failed and open the build log.
  3. Scroll up from the exit code line to the first line containing error, failed, Cannot find, Can't resolve or Module not found.
  4. Copy that line and the few lines around it. That is what you search for and what you give to support if needed.

Netlify also offers a Why did it fail? button on the deploy details page. It is a quick second opinion, but check its suggestion against the log yourself.

2. Fix a case mismatch (ENOENT, "Module not found")

  1. Compare the path in the error with the real file name, letter by letter, including capitals and extensions.
  2. Run git ls-files and look at the name git actually tracks. This can differ from what you see in Finder or Explorer.
  3. If git still tracks the old capitalization, rename in two steps so git records the change:
git mv Old.png temp.png
git mv temp.png new.png
git commit -m "Fix file name case"
  1. Make every import and src/href match the tracked name exactly, then push.

3. Check that every needed file is committed

  1. Run git status and look for untracked files the build uses.
  2. Run git check-ignore -v path/to/file to see whether .gitignore excludes a file you need.
  3. Clone your repo into a new empty folder and run the build there. That copy contains only what Netlify gets.

4. Reproduce with a clean install

  1. In that fresh clone (or after deleting node_modules), install from the lockfile. With npm that is npm ci.
  2. Run the same build command Netlify uses, for example npm run build.
  3. If it fails now, you have found the mismatch. Fix it, and commit the lockfile so Netlify installs the same versions.

If the log shows Could not resolve dependency or Conflicting peer dependency, Netlify's troubleshooting tips describe this for some npm versions with React 17 or later. They suggest setting the NPM_FLAGS environment variable to pass either --force or --legacy-peer-deps to the npm install command. See Netlify's build troubleshooting tips.

5. Match the Node version

Netlify lets you add a .node-version or .nvmrc file to the site's base directory, or set a NODE_VERSION environment variable. A file or variable overrides the setting in the UI, and the docs list the order of precedence as .nvmrc, .node-version, NODE_VERSION, then the UI. Put the version you use locally in .nvmrc, commit it, and redeploy. Details are in Netlify's dependency management docs.

6. Add missing environment variables

Add each variable your build reads in the site's environment variable settings, then redeploy. Remember that a value in a local .env file is not used on Netlify.

One more trap: Netlify's docs say that if you set NODE_ENV to production, any devDependencies in your package.json file will not be installed for the build. If your build tool lives in devDependencies, it will then be missing.

7. Check the build settings

Open the site's build settings and check the build command, the publish directory and, in a monorepo, the base directory. If you keep these in netlify.toml, remember that the file can override the UI. A wrong folder produces missing-file errors even when the code is fine.

8. Retry without the cache

If the log points at a stale dependency or cache, deploy again with the "Retry without cache" option. Netlify says it is worth retrying with a cleared build cache after a failure. This helps after dependency changes. It will not fix a real code or path error.

Don't hide the error

Appending || true to the build command can make the step "pass", but you will deploy a broken or empty site. Fix the first error in the log instead.

Cause, symptom and fix at a glance

CauseWhat the log usually showsFix
Case mismatchENOENT, Module not found, Can't resolve with a path that looks rightRename with two git mv steps, match imports
File not committedMissing file that exists locallygit status, check .gitignore, commit it
Dependency driftMissing package or version error after installnpm ci locally, commit the lockfile
Peer dependency conflictCould not resolve dependencySet NPM_FLAGS as in Netlify's tips
Node versionSyntax or engine errors, odd package failures.nvmrc or NODE_VERSION
Missing variableundefined values, auth or API errors in the buildAdd it in Netlify's environment settings
Wrong folder settingsMissing package.json or publish folderFix base, build command and publish directory
devDependencies skippedBuild tool not foundCheck NODE_ENV

Checklist before you push again

  • The first real error line in the log is identified and matches the fix you made.
  • git ls-files shows the exact file name case your code imports.
  • A fresh clone builds with a clean install.
  • The lockfile is committed.
  • .nvmrc or NODE_VERSION matches your local Node.
  • Every variable the build reads exists in Netlify's settings.
  • Build command, publish directory and base directory are correct.

Test from a clean clone

Building in a fresh clone catches uncommitted files, missing lockfile entries and stale node_modules in one go. It is the closest you can get to Netlify's build without deploying.

If nothing works

First, check that the failure is not on Netlify's side. Netlify says that post-processing failures are retried, and after 4 retries the deploy fails and you will probably need to contact support. Netlify's docs also say an exit status 128 typically means they don't have permission to clone the repository, which is a repo-access problem rather than a code problem.

Contact Netlify support (or ask in Netlify's support forum) and include the deploy URL, the first real error lines from the log, your build command, publish directory and Node version, and what you already tried. Here is a message you can copy:

Subject: Build fails with "Build script returned non-zero exit code: 2" (works locally)

Site: <your-site-name>.netlify.app
Deploy ID / URL: <paste the deploy URL>
Time of failure: <date, time, time zone>

First error in the log (above the exit code line):
<paste the error lines>

Build command: <e.g. npm run build>
Publish directory: <e.g. dist>
Base directory: <blank or path>
Node version: <local version> locally, <.nvmrc / NODE_VERSION value> on Netlify

Already tried: fresh clone builds locally with a clean install; checked file
name case with git ls-files; retried without cache.

Please can you tell me what differs in the build environment?

Common questions

Why does it build on my Mac but not on Netlify?

Most often because of case. Your file system accepted import "./logo" for Logo.png, and Linux does not. A file that was never committed is the second most common reason.

Is "exit code 2" a specific error?

No. It is the status returned by the command that failed, so the cause is whatever the log printed just before it.

Does the same advice apply to Vercel?

The clean-clone, case and Node version checks apply on any Linux-based build service. Vercel's docs also say the last "Error" line in a log is often not the informative one, and that a few lines above, you will see an additional error that shows the real problem. See Vercel's build troubleshooting page.

Should I clear the cache every time?

No. Clearing the cache helps when a cached dependency is stale. It will not fix a wrong path, a missing file or a missing variable, so read the log first.

If your site already builds locally and you only need the finished output online, Liite can serve an uploaded static folder or zip without a build step; see uploaded sites.