I get this error when trying to run a Sanity Studio ```Uncaught TypeError: Cannot read property 'sanity' of undefined```
This error typically occurs due to a dependency conflict with styled-components in your Sanity Studio project. The theme object isn't properly structured because different versions of styled-components or @sanity/ui are being loaded.
Common Solutions
1. Delete and Reinstall Dependencies (Most Common Fix)
This resolves the issue in most cases by clearing out conflicting package versions:
# If using npm
rm -rf node_modules package-lock.json
npm install
# If using yarn
rm -rf node_modules yarn.lock
yarn install2. Check for Conflicting styled-components Versions
The error often happens when your dependency tree has both styled-components 5.1.x and 5.2.x. Run:
npm ls styled-componentsIf you see multiple versions or "UNMET PEER DEPENDENCY" warnings, that's your problem. The issue was particularly common in Sanity v2.1.x releases where @sanity/ui@0.28.0 required styled-components@^5.2 but other packages were pulling in 5.1.x.
3. Explicitly Install styled-components 5.2+
Add styled-components to your package.json if it's not already there:
npm install styled-components@^5.24. Check for Incompatible Plugins
Third-party plugins (like older versions of sanity-plugin-tabs, sanity-plugin-content-model-graph, etc.) can cause this issue if they have outdated peer dependencies. Try temporarily removing custom plugins to isolate the problem.
5. Update Sanity Packages
If you're on an older version, update to the latest:
npm install @sanity/base@latest @sanity/cli@latest @sanity/core@latest @sanity/desk-tool@latestOr if you're ready to migrate, consider upgrading to Sanity Studio v3 which has better dependency management.
Why This Happens
The error occurs in GlobalStyle.tsx when it tries to access theme.sanity.color.base, but the theme object doesn't have the expected structure. This happens when:
- Multiple versions of
@sanity/uiorstyled-componentsare installed - The theme provider isn't properly wrapping components due to version mismatches
- A plugin is using an incompatible version of styled-components
According to this GitHub issue, the Sanity team addressed this in later releases by making @sanity/ui and styled-components peer dependencies instead of direct dependencies, which helps prevent version conflicts.
Note: If you're still on Sanity v2, this was a known issue that's been resolved in later v2 releases and completely redesigned in v3. The most reliable fix is deleting node_modules and your lock file, then reinstalling fresh dependencies.
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.