The error message “Netlify deployment failed during stage building site: build script returned non-zero exit code 2” indicates a problem during the build process of a site on Netlify. This issue is significant because it prevents the successful deployment of a website, which can disrupt development workflows and delay project timelines. It is a common occurrence often caused by misconfigurations, missing dependencies, or errors in the build script. Understanding and resolving this error is crucial for maintaining smooth and efficient deployment processes.
Here are some common causes for the error ‘Netlify deployment failed during stage building site build script returned non-zero exit code 2′:
Incorrect Build Commands: Ensure your build command is correctly specified in the Netlify settings. For example, if you’re using Next.js, the command should typically be next build
.
Missing Dependencies: Check that all necessary dependencies are listed in your package.json
and are correctly installed. Sometimes, a missing or outdated dependency can cause the build to fail.
Configuration Issues: Verify your configuration files, such as netlify.toml
or next.config.js
, to ensure they are correctly set up. Incorrect paths or settings can lead to build failures.
Environment Variables: Make sure all required environment variables are set correctly in the Netlify dashboard. Missing or incorrect environment variables can disrupt the build process.
File Path Issues: Ensure that the paths specified in your configuration files and build commands are correct. Incorrect paths can prevent Netlify from finding necessary files.
Plugin Errors: If you’re using Netlify plugins, ensure they are correctly configured and compatible with your project. Misconfigured plugins can cause build failures.
Sure, here are the detailed troubleshooting steps:
Check Build Commands:
netlify.toml
or Netlify UI is correct. Common commands are npm run build
or yarn build
.Verify Dependencies:
npm install
or yarn install
locally.package.json
.Review Configuration Settings:
.next
unless specified otherwise in your next.config.js
.Check Logs:
Local Simulation:
Common Fixes:
next export
, set the environment variable NETLIFY_NEXT_PLUGIN_SKIP
to true
.package.json
is correct, e.g., "build": "react-scripts build"
.Following these steps should help you identify and resolve the issue. If the problem persists, consider reaching out to Netlify support with detailed logs and configuration settings.
Case Study: Missing package.json
File
Build script returned non-zero exit code: 2
npm run build
command couldn’t find the package.json
file in the /opt/build/repo/dist
directory.package.json
file was present in the correct directory before running the npm run build
command.Case Study: Incorrect Build Command for Next.js
Build script returned non-zero exit code: 2
/opt/build/repo/.next
did not contain a Next.js production build. The build command was incorrectly set to npm run
.npm run build
.Case Study: Environment Variable Configuration
Build script returned non-zero exit code: 2
CI
to false
in Netlify’s build settings.These examples highlight common issues and their resolutions, helping to troubleshoot similar errors effectively.
To avoid the error “Netlify deployment failed during stage building site build script returned non-zero exit code 2” in future deployments, consider these preventive measures:
package.json
, netlify.toml
) are correctly set up and in sync with your project structure.package-lock.json
, yarn.lock
) to maintain consistent dependency versions.Implementing these practices can help ensure smoother deployments and reduce the likelihood of encountering this error.
The error ‘Netlify deployment failed during stage building site: build script returned non-zero exit code 2’ is caused by misconfigurations, missing dependencies, or errors in the build script.