Issue with Sanity V3 build failing in production environment in Netlify.

2 replies
Last updated: Aug 23, 2023
Hey Everyone,I recently upgrade my Sanity project from V2 to V3. Although it builds fine in my local machine, the build fails in the production environment in Netlify.
I had this same issue when I was building in QA environment in Netlify, and apparently, it was because the .nvmrc file in Sanity code was of an older version. I fixed that and build was success in QA environment for Netlify. But Now I have the same issue in prod, even with the updated node version.

Issue: During Sanity build, it is trying to access the packages that no longer exists in Sanity V3, i.e,
user F
/core
AI Update

I understand you're facing a frustrating build issue after upgrading from Sanity v2 to v3. Even though your local build works fine and you've updated the Node version in your .nvmrc file, your production Netlify build is still failing because it's trying to access packages that no longer exist in v3 (like @sanity/core and @sanity/base).

This is a common migration issue. Here's what's happening and how to fix it:

The Root Cause

In Sanity v3, several packages were deprecated and removed, including:

  • @sanity/base
  • @sanity/core
  • @sanity/default-layout
  • @sanity/default-login
  • @sanity/desk-tool

These packages have been consolidated into the single sanity package. If your build is still trying to import from these packages, it will fail.

Steps to Fix Your Production Build

1. Remove Deprecated Packages from package.json

Make sure these packages are completely removed from your package.json dependencies:

// Remove these if they exist:
"@sanity/base"
"@sanity/core"
"@sanity/default-layout"
"@sanity/default-login"
"@sanity/desk-tool"

2. Update Your Imports Throughout Your Codebase

Search your entire project for any imports from the old packages and update them. Common patterns to find and replace:

Old v2 imports:

import sanityClient from "part:@sanity/base/client"
import schema from 'part:@sanity/base/schema'
import userStore from "part:@sanity/base/user"

New v3 imports:

import {useClient} from 'sanity'
import {useSchema} from 'sanity'
import {useCurrentUser} from 'sanity'

3. Clear Netlify's Build Cache

Sometimes Netlify caches dependencies. Try:

  • Go to your Netlify site settings
  • Navigate to "Deploys" → "Deploy settings"
  • Click "Clear cache and retry deploy"

4. Check Your Build Command

Make sure your Netlify build command is using the v3 command. It should be:

npm run build
# or
sanity build

5. Verify node_modules Isn't Cached

If you have a node_modules directory committed to your repo (you shouldn't), remove it and add it to .gitignore.

6. Check for Hidden Import References

Search your entire codebase (including config files) for these patterns:

  • part:@sanity/
  • @sanity/base
  • @sanity/core

Common places these might hide:

  • Custom plugins
  • sanity.config.js (should be updated from sanity.json)
  • Custom input components
  • Structure builder configurations

Complete Migration Checklist

According to the Sanity v2 to v3 migration guide, you should:

  1. ✅ Update Node version (you've done this)
  2. ✅ Remove deprecated packages
  3. ✅ Install sanity@latest
  4. ✅ Update dependencies:
    • "@sanity/ui": "^1"
    • "react": "^18.2.0"
    • "react-dom": "^18.2.0"
  5. ✅ Move configuration from sanity.json to sanity.config.js
  6. ✅ Replace all import X from "part:X" statements
  7. ✅ Update any plugins to v3-compatible versions

Quick Debug Steps for Netlify

To identify exactly what's causing the issue:

  1. Check your Netlify build logs for the specific error message
  2. Look for which file is trying to import the deprecated package
  3. Search that file path in your codebase and update the imports

If you're still stuck after trying these steps, please share:

  • The exact error message from your Netlify build logs
  • Your package.json dependencies
  • Any custom plugins you're using

The Migration Cheat Sheet is also a great quick reference for common v2 to v3 code conversions!

This issue got resolve. Just for anyone else facing these kind of issues -• Make sure env variables are correctly provided
• Make sure the updated node version is being used.
Thanks for sharing your solution!

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?