TypeError when running `npm run dev` in a fresh install of Sanity.io

11 replies
Last updated: Jan 10, 2024
Hey all, so I'm trying to install sanity@latest and everything runs fine on the initial setup. But, upon running
npm run dev
I'm met with a TypeError:
Cannot read properties of undefined (reading 'toLowerCase')
mind you this is a fresh install and I have not written a single line yet. Any thoughts?
AI Update

I'm sorry you're running into this error on a fresh install! That's definitely frustrating when you haven't even written any code yet.

Since this is happening on a completely fresh installation, here are the most common causes and solutions to try:

1. Check your Node.js version

Sanity Studio v4 requires Node.js version 20 or higher. If you're on an older version, this could cause unexpected errors. Check your version with:

node -v

If you're below Node 20, upgrade to the latest LTS version and try again.

2. Clear your dependency cache and reinstall

Sometimes package installations can get corrupted or have conflicts. Try a clean reinstall:

# Delete node_modules and lock file
rm -rf node_modules
rm package-lock.json  # or yarn.lock or pnpm-lock.yaml

# Clear npm cache
npm cache clean --force

# Reinstall dependencies
npm install

3. Check for environment-specific issues

The toLowerCase() error often happens when code is trying to access browser APIs (like navigator) in an environment where they don't exist. This can happen if:

  • You're using a package manager or Node version with compatibility issues
  • There's a conflict with other global packages
  • Your terminal or environment has unusual configurations

4. Try creating a new project

If the above doesn't work, try creating a completely fresh project in a new directory to rule out any local environment issues:

npm create sanity@latest

Follow the prompts for a new project and see if the error persists.

5. Check what version you actually installed

Make sure you're getting the latest stable version by checking your package.json. The sanity package should be at version 3.x or 4.x (depending on when you're reading this).

If none of these solutions work, it would be helpful to share more details like:

  • Your exact Node.js version
  • Your package manager (npm, yarn, or pnpm) and its version
  • Your operating system
  • The complete error stack trace if possible

You might also want to check the Sanity GitHub issues to see if others have reported this specific error recently, or reach out in the Sanity Slack community where the team and community can help troubleshoot in real-time.

This is what the error looks like upon running
npm run dev
Are you on the latest version of the CLI? What does your
sanity.config
file look like?
The latest version of the CLI and it's the default config. I haven't changed a single line and it looks like the error is coming from the node modules.
Hi, I have the same issiue. I'm just following the Sanity Crash Course tutorial.
I too have this issue with a fresh project following Getting Started guide.
I resolved this by going to
node_modules/framer-motion/dist/cjs/index.js
line 4223 and changing from:
navigator.userAgent.toLowerCase().includes(string);
to:

navigator?.userAgent?.toLowerCase().includes(string);

It was then possible to get the development server up and running
I will definitely give that a try and keep you posted.
user R
that worked like a charm, thank you so much!!
This was also my problem! Thanks a lot πŸ‘
Same issue here I had to
pnpm patch
framer-motion as
user R
did.
Thanks to
user R
for pointing this out, had exactly the same problem. BUT just to clarify the file has been updated & is now on line 4257

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?