Issue deploying site to Netlify due to missing dependencies

4 replies
Last updated: Jun 16, 2021
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 settingsBuild & deployContinuous 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 settingsEnvironment 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! 🚀

Thank you for your reply
user A
,
7:34:28 PM: build-image version: 0582042f4fc261adc7bd8333f34884959c577302
7:34:28 PM: build-image tag: v3.7.6
7:34:28 PM: buildbot version: 46ee531a6ee957f94fb29adfde3b52177c7d5147
7:34:28 PM: Building without cache
7:34:28 PM: Starting to prepare the repo for build
7:34:29 PM: No cached dependencies found. Cloning fresh repo
7:34:29 PM: git clone git@github.com:mxysaj/launchblog
7:34:30 PM: Preparing Git Reference refs/heads/main
7:34:31 PM: Parsing package.json dependencies
7:34:31 PM: Different build dir detected, going to use the one specified in the Netlify configuration file: '' versus '/' in the Netlify UI
7:34:32 PM: Starting build script
7:34:32 PM: Installing dependencies
7:34:32 PM: Python version set to 2.7
7:34:33 PM: v12.18.0 is already installed.
7:34:34 PM: Now using node v12.18.0 (npm v6.14.4)
7:34:34 PM: Started restoring cached build plugins
7:34:34 PM: Finished restoring cached build plugins
7:34:34 PM: Attempting ruby version 2.7.1, read from environment
7:34:35 PM: Using ruby version 2.7.1
7:34:36 PM: Using PHP version 5.6
7:34:36 PM: Started restoring cached node modules
7:34:36 PM: Finished restoring cached node modules
7:34:36 PM: Installing NPM modules using NPM version 6.14.4
7:34:54 PM: npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!
7:34:54 PM: > sanity-gatsby-blog@1.0.17 postinstall /opt/build/repo
7:34:54 PM: > lerna bootstrap
7:34:54 PM: lerna notice cli v3.22.1
7:34:54 PM: lerna info ci enabled
7:34:55 PM: lerna info Bootstrapping 2 packages
7:34:55 PM: lerna info Installing external dependencies
7:34:56 PM: lerna ERR! npm ci exited 1 in 'blog-with-gatsby-studio'
7:34:56 PM: lerna ERR! npm ci stderr:
7:34:56 PM: npm ERR! cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
7:34:56 PM: npm ERR!
7:34:56 PM: npm ERR!
7:34:56 PM: npm ERR! Missing: @sanity/code-input@^2.9.0
7:34:56 PM: npm ERR!
7:34:56 PM: npm ERR! A complete log of this run can be found in:
7:34:56 PM: npm ERR!     /opt/buildhome/.npm/_logs/2021-06-14T15_34_56_063Z-debug.log
7:34:56 PM: lerna ERR! npm ci exited 1 in 'blog-with-gatsby-studio'
7:34:56 PM: lerna WARN complete Waiting for 1 child process to exit. CTRL-C to exit immediately.
7:34:56 PM: npm ERR! code ELIFECYCLE
7:34:56 PM: npm ERR! errno 1
7:34:56 PM: npm ERR! sanity-gatsby-blog@1.0.17 postinstall: `lerna bootstrap`
7:34:56 PM: npm ERR! Exit status 1
7:34:56 PM: npm ERR!
7:34:56 PM: npm ERR! Failed at the sanity-gatsby-blog@1.0.17 postinstall script.
7:34:56 PM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
7:34:56 PM: npm ERR! A complete log of this run can be found in:
7:34:56 PM: npm ERR!     /opt/buildhome/.npm/_logs/2021-06-14T15_34_56_164Z-debug.log
7:34:56 PM: Error during NPM install
7:34:56 PM: Build was terminated: Build script returned non-zero exit code: 1
7:34:56 PM: Creating deploy upload records
7:34:56 PM: Failing build: Failed to build site
7:34:56 PM: Failed during stage 'building site': Build script returned non-zero exit code: 1
7:34:56 PM: Finished processing build request in 27.800705191s

Try deleting your node_modules and re-running
npm install
Then push to GitHub again
Thanks
user C
for your reply. I somehow managed to solve the issue. I guess this was because not all the dependencies were in the top-level
package.json
file. I created a new repo and reinstalled everything and now its up and running 🙌

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?