# Build failed with Build script returned non-zero exit code: 4

**URL:** <https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506>\
**Category:** Support\
**Tags:** building, deployment\
**Created:** [December 12, 2023, 11:26pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506 "2023-12-12T23:26:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![garethf](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/garethf/32/23544_2.png) [@garethf](https://answers.netlify.com/u/garethf)\
**Post date:** [December 12, 2023, 11:26pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/1 "2023-12-12T23:26:55Z")

</div>

Hi,

I am getting the error `Build script returned non-zero exit code: 4` on a build task that actually builds correctly.

This exact repo was used to build a website a week ago on another Netlify site and worked correcly with the same env variables and settings.

I know it’s old versions of Node and Gatsby etc. but they still should build and deploy.

Can anyone please help with this?

Log:

```auto
10:53:19 PM: build-image version: 3ffff9df3d5419545acc1b673a54de348174406d (focal)
10:53:19 PM: buildbot version: 06bd328e5eb3b6956f9c6602486ecccce8b861a0
10:53:19 PM: Fetching cached dependencies
10:53:19 PM: Failed to fetch cache, continuing with build
10:53:19 PM: Starting to prepare the repo for build
10:53:19 PM: No cached dependencies found. Cloning fresh repo

10:53:20 PM: Preparing Git Reference refs/heads/master
10:53:21 PM: Starting to install dependencies
10:53:21 PM: Python version set to 3.8
10:53:21 PM: Attempting Ruby version 2.7.2, read from environment
10:53:22 PM: Using Ruby version 2.7.2
10:53:23 PM: Started restoring cached go cache
10:53:23 PM: Finished restoring cached go cache
10:53:24 PM: go version go1.19.13 linux/amd64
10:53:24 PM: Using PHP version 8.0
10:53:25 PM: Downloading and installing node v14.21.3...
10:53:26 PM: Downloading https://nodejs.org/dist/v14.21.3/node-v14.21.3-linux-x64.tar.xz...
10:53:26 PM: Computing checksum with sha256sum
10:53:26 PM: Checksums matched!
10:53:28 PM: Now using node v14.21.3 (npm v6.14.18)
10:53:28 PM: Enabling Node.js Corepack
10:53:28 PM: Started restoring cached build plugins
10:53:28 PM: Finished restoring cached build plugins
10:53:28 PM: Started restoring cached corepack dependencies
10:53:28 PM: Finished restoring cached corepack dependencies
10:53:28 PM: Started restoring cached yarn cache
10:53:28 PM: Finished restoring cached yarn cache
10:53:28 PM: No yarn workspaces detected
10:53:28 PM: Started restoring cached node modules
10:53:28 PM: Finished restoring cached node modules
10:53:28 PM: Installing npm packages using Yarn version 1.22.19
10:53:29 PM: yarn install v1.22.19
10:53:29 PM: [1/4] Resolving packages...
10:53:29 PM: [2/4] Fetching packages...
10:53:34 PM: warning url-loader@1.1.2: Invalid bin field for "url-loader".
10:54:00 PM: [3/4] Linking dependencies...
10:54:00 PM: warning "gatsby > @pmmmwh/react-refresh-webpack-plugin@0.3.3" has incorrect peer dependency "react-refresh@^0.8.2".
10:54:00 PM: warning " > babel-loader@8.1.0" has unmet peer dependency "webpack@>=2".
10:54:00 PM: warning "gatsby > @typescript-eslint/eslint-plugin > tsutils@3.17.1" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
10:54:00 PM: warning "gatsby > gatsby-admin > gatsby-interface@0.0.163" has incorrect peer dependency "gatsby@2.6.0".
10:54:00 PM: warning "gatsby > gatsby-admin > gatsby-interface@0.0.163" has incorrect peer dependency "react@16.8.1".
10:54:00 PM: warning "gatsby > gatsby-admin > gatsby-interface@0.0.163" has incorrect peer dependency "react-dom@16.8.1".
10:54:00 PM: warning "gatsby > gatsby-admin > gatsby-interface@0.0.163" has incorrect peer dependency "yup@^0.27.0".
10:54:00 PM: warning "gatsby-plugin-netlify > webpack-assets-manifest@3.1.1" has unmet peer dependency "webpack@>=4.4.0".
10:54:00 PM: warning " > gatsby-plugin-theme-ui@0.3.0" has incorrect peer dependency "theme-ui@^0.2.0".
10:54:00 PM: warning "@storybook/addon-a11y > @storybook/api@5.3.19" has unmet peer dependency "regenerator-runtime@*".
10:54:00 PM: warning " > eslint-config-gatsby-standard@2.2.0" has incorrect peer dependency "eslint@^5.16.0".
10:54:00 PM: warning "eslint-config-gatsby-standard > eslint-plugin-react@7.19.0" has incorrect peer dependency "eslint@^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0".
10:54:28 PM: [4/4] Building fresh packages...
10:54:45 PM: Done in 76.34s.
10:54:45 PM: npm packages installed using Yarn
10:54:45 PM: Successfully installed dependencies
10:54:45 PM: Starting build script
10:54:46 PM: Detected 1 framework(s)
10:54:46 PM: "gatsby" at version "2.22.13"
10:54:46 PM: Section completed: initializing
10:54:48 PM: ​
10:54:48 PM: Netlify Build                                                 
10:54:48 PM: ────────────────────────────────────────────────────────────────
10:54:48 PM: ​
10:54:48 PM: ❯ Version
10:54:48 PM: @netlify/build 29.31.0
10:54:48 PM: ​
10:54:48 PM: ❯ Flags
10:54:48 PM: baseRelDir: true
10:54:48 PM: buildId: 6578e45c97fa131d7013e381
10:54:48 PM: deployId: 6578e45c97fa131d7013e383
10:54:48 PM: ​
10:54:48 PM: ❯ Current directory
10:54:48 PM: /opt/build/repo
10:54:48 PM: ​
10:54:48 PM: ❯ Config file
10:54:48 PM: No config file was defined: using default values.
10:54:48 PM: ​
10:54:48 PM: ❯ Context
10:54:48 PM: production
10:54:48 PM: ​
10:54:48 PM: ❯ Installing plugins
10:54:48 PM: - @netlify/plugin-gatsby@3.8.0
10:55:05 PM: ​
10:55:05 PM: ❯ Loading plugins
10:55:05 PM: - @netlify/plugin-gatsby@3.8.0 from Netlify app
10:55:06 PM: ​
10:55:06 PM: @netlify/plugin-gatsby (onPreBuild event)                     
10:55:06 PM: ────────────────────────────────────────────────────────────────
10:55:06 PM: ​
10:55:06 PM: No Gatsby cache found. Building fresh.
10:55:06 PM: The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0
10:55:06 PM: ​
10:55:06 PM: (@netlify/plugin-gatsby onPreBuild completed in 12ms)
10:55:06 PM: ​
10:55:06 PM: Build command from Netlify app                                
10:55:06 PM: ────────────────────────────────────────────────────────────────
10:55:06 PM: ​
10:55:06 PM: $ CI="" yarn previewSetup && yarn run build:preview
10:55:06 PM: yarn run v1.22.19
10:55:06 PM: $ cp -f preview.toml netlify.toml
10:55:06 PM: Done in 0.04s.
10:55:07 PM: yarn run v1.22.19
10:55:07 PM: $ gatsby build
10:55:08 PM: success open and validate gatsby-configs - 0.033s
10:55:09 PM: success load plugins - 0.430s
10:55:09 PM: success onPreInit - 0.007s
10:55:09 PM: success delete html and css files from previous builds - 0.008s
10:55:09 PM: success initialize cache - 0.004s
10:55:09 PM: success copy gatsby files - 0.017s
10:55:09 PM: success onPreBootstrap - 0.005s
10:55:09 PM: info Reading GraphQL type definitions from /opt/build/repo/src/gatsby/schema/schema.gql
10:55:09 PM: success createSchemaCustomization - 0.009s
10:55:09 PM: Starting to fetch data from Contentful
10:55:09 PM: info Fetching default locale
10:55:09 PM: info default locale is: en-GB
10:55:10 PM: info contentTypes fetched 10
10:55:10 PM: info Updated entries 67
10:55:10 PM: info Deleted entries 0
10:55:10 PM: info Updated assets 72
10:55:10 PM: info Deleted assets 0
10:55:10 PM: Fetch Contentful data: 657.726ms
10:55:10 PM: success source and transform nodes - 0.920s
10:55:10 PM: success building schema - 0.438s
10:55:10 PM: success createPages - 0.026s
10:55:10 PM: success createPagesStatefully - 0.043s
10:55:10 PM: success onPreExtractQueries - 0.001s
10:55:10 PM: success update schema - 0.015s
10:55:11 PM: success extract queries from components - 0.329s
10:55:11 PM: success write out requires - 0.005s
10:55:11 PM: success write out redirect data - 0.001s
10:55:11 PM: success Build manifest and related icons - 0.138s
10:55:11 PM: success onPostBootstrap - 0.139s
10:55:11 PM: ⠀
10:55:11 PM: info bootstrap finished - 3.998s
10:55:11 PM: ⠀
10:55:11 PM: warning Browserslist: caniuse-lite is outdated. Please run:
10:55:11 PM: npx browserslist@latest --update-db
10:55:29 PM: success Building production JavaScript and CSS bundles - 17.782s
10:55:29 PM: success Rewriting compilation hashes - 0.001s
10:55:29 PM: success run queries - 18.328s - 21/21 1.15/s
10:55:32 PM: success Building static HTML for pages - 2.903s - 13/13 4.48/s
10:55:32 PM: success Generating image thumbnails - 21.273s - 2/2 0.09/s
10:55:32 PM: success onPostBuild - 0.005s
10:55:32 PM: info Done building in 25.291605932 sec
10:55:32 PM: Done in 25.56s.
10:55:32 PM: Unhandled rejection…
10:55:32 PM: TypeError: Cannot read properties of undefined (reading "packageName")
10:55:32 PM: at title (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/error/types.js:281:31)
10:55:32 PM: at getTitle (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/error/parse/parse.js:69:12)
10:55:32 PM: at getFullErrorInfo (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/error/parse/parse.js:14:20)
10:55:32 PM: at logBuildError (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/log/messages/core.js:17:27)
10:55:32 PM: at handleBuildError (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/error/handle.js:17:9)
10:55:32 PM: at async file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/core/main.js:81:34
10:55:32 PM: at async buildSite (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/core/main.js:31:12)
10:55:32 PM: at async runCli (file:///opt/buildhome/node-deps/node_modules/@netlify/build/lib/core/bin.js:22:36)
10:55:32 PM: /opt/buildhome/node-deps/node_modules/.bin/netlify-build exited with exit code 0 without finishing the build.
10:55:33 PM: Build failed due to an internal system error: Build script returned non-zero exit code: 4
10:55:33 PM: Failing build: Failed to build site
10:55:33 PM: Finished processing build request in 2m14.126s
10:55:33 PM: Failed during stage "building site": Build script returned non-zero exit code: 4

```

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [December 14, 2023, 6:57pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/2 "2023-12-14T18:57:00Z")

</div>

what site are you writing in about?

---

<div class="post-metadata">

**Author:** ![garethf](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/garethf/32/23544_2.png) [@garethf](https://answers.netlify.com/u/garethf)\
**Post date:** [December 18, 2023, 10:58am UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/3 "2023-12-18T10:58:08Z")

</div>

@SamO Thanks for the response, it is [alex-preview.netlify.app](https://alex-preview.netlify.app)

---

<div class="post-metadata">

**Author:** ![Melvin](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/melvin/32/23606_2.png) [@Melvin](https://answers.netlify.com/u/Melvin)\
**Post date:** [December 18, 2023, 6:47pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/4 "2023-12-18T18:47:09Z")

</div>

Hi @garethf,

Thanks for the follow-up.

Looks like the error is here:

[https://app.netlify.com/sites/alex-preview/deploys/6578e7a15f54ec1fc0bfc789#L147](https://app.netlify.com/sites/alex-preview/deploys/6578e7a15f54ec1fc0bfc789#L147)

I believe the error lies with the code. This error occurs when a property is read or a function is called on an undefined variable. Have you declared that variable but not used it in your code anywhere?

This Support Guide has a lot of debugging tips that may help:

> [@\[Support Guide\] Frequently encountered problems during builds](https://answers.netlify.com/t/support-guide-frequently-encountered-problems-during-builds/213):
>
> Last reviewed by Netlify Support - July 2025 Overview Sites on Netlify can be built using a variety of tools — different frameworks, programming languages and approaches. It can be incredibly frustrating to see your site fail to build! If you have a build on Netlify that is failing with an error message that you don’t understand, you are not alone. Here you’ll find some common build failure messages, and probable solutions to their causes. If you’re looking for more general information on build…

If you’re still having issues, please let us know if you’re able to deploy locally.

---

<div class="post-metadata">

**Author:** ![garethf](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/garethf/32/23544_2.png) [@garethf](https://answers.netlify.com/u/garethf)\
**Post date:** [December 20, 2023, 11:08pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/5 "2023-12-20T23:08:45Z")

</div>

@Melvin Thanks for the response, this error was actually being caused by the netlify.toml file I was generating on build time to hide this site from search engines. I couldn’t see anything wrong with the syntax but this was causing the error.

Are there any settings in Netlify to keep this private?

---

<div class="post-metadata">

**Author:** ![Melvin](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/melvin/32/23606_2.png) [@Melvin](https://answers.netlify.com/u/Melvin)\
**Post date:** [December 21, 2023, 1:15pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/6 "2023-12-21T13:15:41Z")

</div>

Hi @garethf,

Thanks for following up.

Do you mean to keep this form post private or do you mean to keep your site private?

---

<div class="post-metadata">

**Author:** ![matthew-peterson-39](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/matthew-peterson-39/32/67216_2.png) [@matthew-peterson-39](https://answers.netlify.com/u/matthew-peterson-39)\
**Post date:** [January 30, 2025, 3:12pm UTC](https://answers.netlify.com/t/build-failed-with-build-script-returned-non-zero-exit-code-4/108506/7 "2025-01-30T15:12:39Z")

</div>

I experienced this issue earlier today. I was able to solve it by running the build command on the branch that was experiencing the issue, for me it was ‘npm run build’, pushed these changes and then redployed without any issue. Hopefully this helps others in the same boat!
