Joint session with Vercel: How to build intelligent storefronts (May 15th)

Netlify deployment error with Sanity-Gatsby blog starter (build script exit code 2)

4 repliesLast updated: Nov 29, 2025

Hi everyone, I am having an issue with deploying my site to Netlify. I am getting this error:
Failed during stage 'building site': Build script returned non-zero exit code: 2

I am using the sanity-gatsby blog starter. Been tweaking it locally, and all was working fine. When I committed my changes to my repo, and deployed my site on netlify I got this error. I must have done something wrong..I am new to all of this and would greatly appreciate any help. Thank you
😊
AI Update

Hey there! Sorry to hear you're running into deployment issues with the Sanity Gatsby blog starter on Netlify. This is actually a pretty common stumbling block, especially since the starter uses a monorepo structure with both the Gatsby site and Sanity Studio in the same repository. Let me walk you through what's likely happening and how to fix it.

The Problem: Monorepo Structure

The sanity-gatsby blog starter has two main folders:

When Netlify tries to build, it needs to know which folder to build from and where to find the built files. If these aren't configured correctly, you'll get that "non-zero exit code: 2" error.

The Solution: Configure Netlify Settings

You need to set two key settings in Netlify:

Here's how to fix it:

Option 1: Through Netlify UI

Option 2: Using netlify.toml (Better!)

Add a netlify.toml file to the root of your repository with this configuration:

[build]
  base = "web"
  publish = "web/public"
  command = "npm run build"

Don't Forget Environment Variables!

The Gatsby site needs to connect to your Sanity project. Make sure you've added these environment variables in Netlify (under Site settingsEnvironment variables):

You can find your project ID in your Sanity project dashboard or in studio/sanity.json.

Additional Troubleshooting

If you're still getting errors after setting the base directory:

The guide on deploying Sanity Studio to Netlify in a monorepo has more details if you're also trying to deploy the Studio separately.

Quick Checklist

Try these settings and redeploy. The build should complete successfully! If you're still stuck, share the specific error from your Netlify build log and we can dig deeper. Good luck! 🚀

Show original thread
4 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions