Error deploying project to Vercel due to missing module '@sanity/cli'
The issue you're experiencing is that Vercel is trying to compile your sanity.cli.ts file as part of your Next.js build process. The sanity.cli.ts file is only meant for running CLI commands (like sanity start or sanity deploy) and shouldn't be included when building your Next.js application.
The Real Problem:
TypeScript is processing your Sanity configuration files during the Next.js build, which causes it to look for @sanity/cli type definitions. This is a project structure issue, not a dependency installation problem.
Solution 1: Set the correct Root Directory in Vercel (Recommended)
If you have your Sanity Studio and Next.js app in separate folders (common monorepo setup), you need to tell Vercel to only build your Next.js app:
- Go to your Vercel project settings → General
- Find the Root Directory setting
- Set it to your Next.js folder (e.g.,
nextjs,web, or whatever your Next.js directory is named) - Save and redeploy
This prevents Vercel from processing any Sanity Studio files during the build.
Solution 2: Exclude Sanity files from TypeScript compilation
If your project structure has Sanity files in the same directory as Next.js, modify your tsconfig.json in the Next.js root to exclude the Sanity configuration:
{
"exclude": ["node_modules", "sanity.cli.ts", "sanity.config.ts", "sanity/**/*"]
}This tells TypeScript to ignore these files during compilation.
Solution 3: Separate your projects completely
The cleanest approach is to have separate repositories or clearly separated directories:
/
├── studio/ (Sanity Studio with its own package.json)
│ ├── sanity.cli.ts
│ ├── sanity.config.ts
│ └── package.json
└── web/ (Next.js app with its own package.json)
├── pages/
├── next.config.js
└── package.jsonThen deploy them as separate Vercel projects or use the Root Directory setting mentioned in Solution 1.
Why moving @sanity/cli to devDependencies won't help:
Vercel installs both dependencies and devDependencies during builds, so moving packages between these sections won't resolve the TypeScript compilation error. The issue is that the file is being compiled at all, not that the dependency is missing.
Quick Check:
Looking at your dependencies, I notice you're running Sanity CLI v2.35.3 but have sanity: ^3.0.0 in your package.json. This is a version mismatch. If you're using Sanity v3 (which you should be with those dependencies), make sure you're actually running the v3 CLI. Run npm list @sanity/cli to verify which version is actually installed.
After implementing one of these solutions, commit and push to trigger a new Vercel deployment. Solution 1 (setting the Root Directory) is the most straightforward if you have a monorepo structure.
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.