Issue deploying site to Netlify due to missing dependencies
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 frontendstudio/- 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:
- Base directory:
web - Publish directory:
web/public
Here's how to fix it:
Option 1: Through Netlify UI
- Go to your site in Netlify
- Navigate to Site settings → Build & deploy → Continuous Deployment
- 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)
- Set Base directory to
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 IDSANITY_DATASET- UsuallyproductionSANITY_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:
- Check your package.json: Make sure your build scripts are correct in the
web/folder - Review the build logs: Netlify's deploy log will show you exactly what's failing
- Verify gatsby-source-sanity: Ensure it's properly configured in your
gatsby-config.jswith your project ID and dataset - Check for
.env.production: Make sure you have this file in yourweb/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! 🚀
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.