Error reading sanity/cli after updating to version 3.35.2

12 replies
Last updated: Mar 26, 2024
Hi there!
I run into this error once I’ve updated the sanity/cli from 3.35.1 to 3.35.2:

> Error reading “/Users/xxxx/xxxx/studio/sanity.cli.ts”: Cannot read properties of undefined (reading ‘div’)
> TypeError: Cannot read properties of undefined (reading ‘div’)
> at Object.
anonymous (/Users/xxxx/neww/xxxx/node_modules/@sanity/ui/dist/index.js:1235:40)
AI Update

This is a known issue with @sanity/cli version 3.35.2 that's related to a styled-components peer dependency problem. The error occurs because version 3.35.2 introduced a requirement for styled-components v6, but your project likely has an older version installed or the dependencies weren't properly resolved during the update.

The Solution

Based on the community discussion, here's how to fix it:

  1. Update styled-components to version 6 in your package.json:

    "styled-components": "^6"
  2. Clean install your dependencies:

    # Delete your lock file and node_modules
    rm package-lock.json
    rm -rf node_modules
    
    # Reinstall everything
    npm install

The key here is doing a fresh npm install rather than npm update. Simply updating won't properly resolve the peer dependency tree, which is why you're seeing the "Cannot read properties of undefined (reading 'div')" error - @sanity/ui is trying to use styled-components methods that aren't available in older versions.

Why This Happened

The @sanity/cli 3.35.2 release updated its dependency on @sanity/ui, which in turn requires styled-components v6. If you had v5 installed, the package wasn't compatible, causing the bundled UI components to fail when trying to access styled-components' API.

Alternative Solution

If you're still experiencing issues after the clean install, you could temporarily downgrade to @sanity/cli 3.35.1 until you're ready to update all dependencies, though upgrading styled-components is the recommended long-term fix. The issue was resolved in subsequent CLI releases, so you might also consider updating to a newer version of @sanity/cli (3.35.3 or later) which should handle the styled-components dependency more gracefully.

having same issue here
user D
Right! I’ll let you know if I’ve found a solution!
I believe this is a
styled-components
issue that’s being fixed in the next release. Can you try manually installing that package?
I’ve just done that, and now I get another error

Error reading “/xxxx/xxxxx/neww/studio/sanity.cli.ts”: avatarStyle is not defined
ReferenceError: avatarStyle is not defined
at Object.<anonymous> (/Users/damonchan/neww/studio/node_modules/@sanity/ui/dist/index.js3)
What does your sanity.cli file look like?
Also is your styled components set to:
"styled-components": "^6"
Yes, I’ve done that!
This is my sanity.cli:

> import {defineCliConfig} from ‘sanity/cli’
> import {projectId, dataset} from ‘./sanity.config’
>
> export default defineCliConfig({
> api: {
> projectId,
> dataset,
> },
> })
Hmmm, ok, double check that you’ve set styled components to
^6
in your
package.json
, delete your
package-lock
and
node_modules
, the run
npm i
to see if that gets it working.
Ah! It works!
Thx! I’ve tried the same thing but I did npm update instead….npm i does the trick
Excellent!
user Q
^

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?