Cannot read property 'sanity' of undefined in Sanity Studio

30 replies
Last updated: Feb 4, 2021
Uncaught error
Cannot read property 'sanity' of undefined

Stack:

TypeError: 
    at <https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:1163:3413841>
    at ye (<https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:552292>)
    at ye (<https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:552065>)
    at e.t.createStyles (<https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:558162>)
    at e.t.renderStyles (<https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:558401>)
    at u (<https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:558961>)
    at <https://dolcestilcriollo.sanity.studio/static/js/app.bundle.js?f8e5bbd157acfb86fc43:30:558777>
    at ro (<https://dolcestilcriollo.sanity.studio/static/js/vendor.bundle.js?2163562f6812e454f877:17:83208>)
    at lo (<https://dolcestilcriollo.sanity.studio/static/js/vendor.bundle.js?2163562f6812e454f877:17:83311>)
    at pu (<https://dolcestilcriollo.sanity.studio/static/js/vendor.bundle.js?2163562f6812e454f877:17:101245>)

(Your browsers Developer Tools may contain more info)
AI Update

This error Cannot read property 'sanity' of undefined is a known issue from Sanity Studio v2 that's related to theme configuration and dependency conflicts with styled-components. Here's how to fix it:

Quick Solutions

1. Delete dependencies and reinstall (Most reliable fix)

# Remove lock file and node_modules
rm -rf node_modules package-lock.json
# or if using yarn:
rm -rf node_modules yarn.lock

# Reinstall everything
npm install
# or
yarn install

This resolved the issue for multiple users experiencing the same problem.

2. Upgrade to the latest Sanity version

The error was particularly common in Sanity v2.1.x through v2.5.x. Updating often fixes it:

sanity upgrade

One user confirmed that upgrading from v2.5.0 to the latest version solved the issue.

3. Check for plugin conflicts

If the error appeared after installing a plugin (like sanity-plugin-power-table or sanity-plugin-tabs):

  • Temporarily remove the plugin to confirm it's the cause
  • Update to the latest Sanity version (plugins may require newer versions)
  • Check if the plugin itself has updates

What Caused This?

The root issue was a dependency mismatch in Sanity's dependency tree:

  • @sanity/form-builder depended on @sanity/ui@^0.28.0
  • @sanity/ui@0.28.0 required styled-components@^5.2 as a peer dependency
  • But the installation was pulling in both v5.1 and v5.2 of styled-components

This created a situation where the theme object structure wasn't what the code expected, causing theme.sanity to be undefined.

The Sanity team fixed this in later versions by making @sanity/ui and styled-components peer dependencies instead of direct dependencies, which resolved the version conflicts.

If You're on Studio v3

If you're using Sanity Studio v3 (the current version), this issue shouldn't occur. Make sure you're running:

  • Node.js 20 or higher
  • The latest sanity package (npm install sanity@latest)

The quickest fix is usually deleting your lock file and node_modules, then reinstalling fresh. If that doesn't work, upgrade to the latest Sanity version.

Show original thread
30 replies
wild guess: have you set
project.basepath
in your sanity.json?
Currently:
"basePath": "/"
The setup is Gatsby in a web folder and the studio in a separate studio folder.
have you tried removing node_modules and package-lock and reinstall?
I am currently going through this process
I tried it another time already to no avail, but wanted to give it a shot again.
Give me a second. I will report back in a minute.
By the way, thank you a lot for jumping in that fast. You are awesome 🎉
So far still the same result, but I also can't find the node_module folder by any means. Is this referring to a node_module folder that should be generated by the studio or gatsby?
I didn't fire up gatsby so far, which should be no problem?
Additional info from the devtools:
The deploy doesn't complain and seems to be successful:
and are all your packages up to date?
sanity upgrade
I will run it 👍
Yes, they are.
everyone here suggests deleting node modules, but if that doesn't work… 😕 https://github.com/sanity-io/sanity/issues/2190
I went through that thread as well.
This is the only hit I got for this issue.
local build succeeds?
Yes
No warnings or anything.
I also deployed the studio to test, same issue.
Successful deploy, same error.
I've got to go, sorry! Maybe someone can else jump in 🙂
testCookie gives me a 404, that could be related?
Sure. Thank you for the input, Arjen!
Okay, resolved it. I thought, that the node_module folder wasn't initialized or that it simply would be somewhere else (who knows why), but I explicitly ignored it in vscodium. Node_module folder and package-lock.json deleted, reinstalled packages and it works like a charm (in Firefox at least, while Chromium just didn't want to accept the login cookie). Thank you for the input Arjen and I hope you excuse my silly oversight.
Great! Glad to hear you got it working 👍

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?