Cloning Sanity project: Cannot find module 'sanity' with pnpm

3 replies
Last updated: Jun 15, 2023
hey sanity peeps! I'm trying to clone an existing project onto a new computer. My other one is having keyboard issues. I decided to try pnpm, but simply cloning the repo and running pnpm (after installing pnpm globally of course) doesn't seem to quite work. I'm getting some red squigglies with the error
Cannot find module 'sanity' or its corresponding type declarations
. Anyone have advice on how to transition from npm to pnpm? And do I have to do anything special as far as sanity setup when cloning an existing repo of mine or should everything be ready to go since I have a cli and config file already defined?
AI Update

Good news - this is usually a simple fix! The "Cannot find module 'sanity'" error you're seeing is typically just TypeScript/your editor being confused before the dependencies are fully installed.

Here's what you need to do when cloning your existing Sanity project and switching to pnpm:

1. Install dependencies with pnpm:

pnpm install

This should install all your dependencies including the sanity package. Since you already have a package.json in your cloned repo, pnpm will read it and install everything.

2. Restart your TypeScript server:

Those red squigglies are often just your editor's TypeScript server not recognizing the newly installed modules. In VS Code:

  • Open the command palette (Cmd/Ctrl + Shift + P)
  • Type "TypeScript: Restart TS Server"
  • Hit enter

This should clear up the error immediately.

3. Clean up old lock files (optional but recommended):

If you're transitioning from npm to pnpm, you might want to remove the old lock file to avoid confusion:

rm package-lock.json

pnpm will create its own pnpm-lock.yaml file.

About cloning existing Sanity projects:

You don't need to do any special Sanity setup when cloning your own repo! Since you already have:

  • Your sanity.config.ts (or .js) file with your project ID
  • Your sanity.cli.ts (or .js) file

Everything should work once dependencies are installed. The Sanity CLI and configuration are project-specific and already in your repo. Just make sure you're authenticated with the CLI (sanity login) if you need to deploy or manage datasets.

One thing to watch out for:

If you happen to have multiple versions of the sanity package installed (rare, but can happen with monorepos or certain dependency configurations), you might see context errors. But in your case with a fresh clone, this shouldn't be an issue.

TL;DR: Run pnpm install, restart your TypeScript server, and you should be good to go! πŸŽ‰

Show original thread
3 replies
Are you on v2 or v3? According to https://github.com/sanity-io/sanity/issues/3417#issuecomment-1347477233 it should technically work
Also, if you have nothing set up via
.env
variables it should work on any machine (granted you can log-in to Sanity in your browser)
Oh for crying out loud! I restarted my typescript server and that error went away. Geez Louise. πŸ€¦β€β™‚οΈ
Thanks though!

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?