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

4 replies
Last updated: Jan 28, 2026
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:

  • web/ - your Gatsby frontend
  • studio/ - your Sanity Studio

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:

  1. Base directory: web
  2. Publish directory: web/public

Here's how to fix it:

Option 1: Through Netlify UI

  1. Go to your site in Netlify
  2. Navigate to Site settings → Build & deploy → Continuous Deployment
  3. Under "Build settings":
    • Set Base directory to web
    • Set Publish directory to web/public
    • Set Build command to npm run build (if not already set)

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 settings → Environment variables):

  • SANITY_PROJECT_ID - Your project ID
  • SANITY_DATASET - Usually production
  • SANITY_TOKEN - If you're using private datasets

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:

  1. Check your package.json: Make sure your build scripts are correct in the web/ folder
  2. Review the build logs: Netlify's deploy log will show you exactly what's failing
  3. Verify gatsby-source-sanity: Ensure it's properly configured in your gatsby-config.js with your project ID and dataset
  4. Check for .env.production: Make sure you have this file in your web/ folder with your Sanity credentials (though environment variables in Netlify are preferred)

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

  • ✅ Base directory set to web
  • ✅ Publish directory set to web/public
  • ✅ Environment variables added in Netlify
  • ✅ Build command is npm run build

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

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.

Was this answer helpful?