Error deploying project to Vercel due to missing module '@sanity/cli'

3 replies
Last updated: Feb 25, 2023
hey guys,
I have a problem when I want to deploy it my project to vercel it gives me this error:


./sanity/sanity.cli.ts:1:31
2023-02-25T16:10:48.112Z  Type error: Cannot find module '@sanity/cli' or its corresponding type declarations.
2023-02-25T16:10:48.112Z  
2023-02-25T16:10:48.112Z  > 1 | import {defineCliConfig} from '@sanity/cli'
2023-02-25T16:10:48.114Z      |                               ^
2023-02-25T16:10:48.114Z    2 | 
2023-02-25T16:10:48.114Z    3 | export default defineCliConfig({
2023-02-25T16:10:48.114Z    4 |   api: {
2023-02-25T16:10:48.165Z  Error: Command "npm run build" exited with 1
I don't know what to do anymore.

This are my package.json dependencies from sanity:


  "dependencies": {
    "@sanity/cli": "^3.4.0",
    "@sanity/vision": "^3.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-is": "^18.2.0",
    "sanity": "^3.0.0",
    "styled-components": "^5.2.0"
  },
  "devDependencies": {
    "@sanity/eslint-config-studio": "^2.0.1",
    "eslint": "^8.6.0",
    "prettier": "^2.8.4",
    "typescript": "^4.0.0"
  },
and from my nextjs:

"dependencies": {
    "@emailjs/browser": "^3.10.0",
    "@heroicons/react": "^2.0.15",
    "@portabletext/react": "^1.0.6",
    "@sanity/image-url": "^1.0.1",
    "framer-motion": "^9.0.2",
    "next": "^12.3.0",
    "next-sanity": "^1.1.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-hook-form": "^7.43.1",
    "react-simple-typewriter": "^5.0.1",
    "react-social-icons": "^5.15.0",
    "react-toastify": "^9.1.1"
  },
  "devDependencies": {
    "@types/node": "18.13.0",
    "@types/react": "18.0.27",
    "@types/react-dom": "18.0.10",
    "autoprefixer": "^10.4.13",
    "eslint": "8.33.0",
    "eslint-config-next": "13.1.6",
    "postcss": "^8.4.21",
    "tailwind-scrollbar": "^2.1.0",
    "tailwindcss": "^3.2.6",
    "typescript": "4.9.5"
  }
I'm using nextjs 12 and not the sanity --version
@sanity/cli version 2.35.3
AI Update

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:

  1. Go to your Vercel project settings → General
  2. Find the Root Directory setting
  3. Set it to your Next.js folder (e.g., nextjs, web, or whatever your Next.js directory is named)
  4. 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.json

Then 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.

user Q
I’m on Svelte, not Nextjs, but I think the
sanity.cli.ts
file should be similar. And in mine I have
import { defineCliConfig } from 'sanity/cli'
without the
@
. Does that work?
no, it doesn't work like that, I already tried it. I think I found the solution, I need it to move the dependencies from sanity package.json to next package.json and do a npm install in root to install them like so:

 "dependencies": {
    "@emailjs/browser": "^3.10.0",
    "@heroicons/react": "^2.0.15",
    "@portabletext/react": "^1.0.6",
    "@sanity/image-url": "^1.0.1",
    "framer-motion": "^9.0.2",
    "next": "^12.3.0",
    "next-sanity": "^1.1.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-hook-form": "^7.43.1",
    "react-simple-typewriter": "^5.0.1",
    "react-social-icons": "^5.15.0",
    "react-toastify": "^9.1.1",
    "@sanity/vision": "^3.5.0",
    "sanity": "^3.0.0",
    "styled-components": "^5.2.0"
  },
  "devDependencies": {
    "@sanity/eslint-config-studio": "^2.0.1",
    "@types/node": "18.13.0",
    "@types/react": "18.0.27",
    "@types/react-dom": "18.0.10",
    "autoprefixer": "^10.4.13",
    "eslint": "8.33.0",
    "eslint-config-next": "13.1.6",
    "postcss": "^8.4.21",
    "tailwind-scrollbar": "^2.1.0",
    "tailwindcss": "^3.2.6",
    "typescript": "4.9.5"
  }

basically copy
"sanity": "^3.0.0"
,
"styled-components": "^5.2.0"
and
"@sanity/vision": "^3.5.0",
in dependencies and
"@sanity/eslint-config-studio": "^2.0.1"
in devDependencies in next package.json, run npm install in next project(not sanity) to install there the dependencies and I got thru that error

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?