Warning and error messages after running sanity start, resolved by performing npm i.
This error is a classic dependency version mismatch issue between @sanity/ui and other Sanity packages after running sanity upgrade. The useMediaIndex export error indicates that you have incompatible versions of Sanity packages installed, where @sanity/ui is trying to import a hook that doesn't exist in the version of its internal dependencies.
Here's how to fix it:
Quick Fix
Delete your node_modules and lockfile:
rm -rf node_modules rm package-lock.json # or yarn.lock / pnpm-lock.yamlReinstall dependencies:
npm install # or yarn / pnpm installRestart the dev server:
sanity start
If that doesn't work:
Check for version mismatches in your package.json. All @sanity/* packages should be on compatible versions. The issue typically happens when:
@sanity/uiis on a different major/minor version than expected by@sanity/baseorsanity- Some packages got upgraded while others didn't
- You have duplicate versions installed (check with
npm list @sanity/ui)
Force align versions by running:
npm install @sanity/ui@latest sanity@latestOr if you're on Studio v2 (which appears to be the case based on @sanity/base in your error), ensure all your @sanity/* packages are on compatible v2 versions. You might need to explicitly install matching versions:
npm install @sanity/ui@^0.x @sanity/base@^2.x(Replace x with the appropriate minor versions that are compatible)
Root Cause
The sanity upgrade command sometimes doesn't upgrade all interdependent packages consistently, especially when there are peer dependency conflicts or when npm/yarn's resolution algorithm picks incompatible versions. The useMediaIndex hook was likely added/moved/removed between versions of @sanity/ui, and your installation has mismatched versions.
Prevention
When upgrading Sanity:
- Always delete
node_modulesand lockfiles after upgrading - Check that all
@sanity/*packages inpackage.jsonare on compatible versions - Run
npm list @sanity/uito verify what version is actually installed - Consider checking the Sanity upgrade documentation for version-specific migration guides
If you continue having issues after trying these steps, check your package.json for any version pinning (~ or exact versions) that might be preventing proper upgrades, and consider sharing your package versions for more specific troubleshooting help.
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.