Trouble deploying to Vercel with Sanity build command
The "sanity: command not found" error on Vercel happens because the Sanity CLI isn't properly installed in your build environment, even though it works locally. Here's how to fix it:
The Core Issue
Your build script uses npx sanity build, but Vercel doesn't have access to the Sanity CLI because:
- The CLI needs to be in your
dependencies(notdevDependencies) - Vercel needs to install dependencies in your
/studiofolder - The
cd studiocommand might not be executing in the context you expect
Solution 1: Move @sanity/cli to dependencies (Recommended)
In your studio/package.json, make sure @sanity/cli and @sanity/core are in dependencies:
{
"name": "studio-project",
"scripts": {
"start": "sanity start",
"build": "sanity build"
},
"dependencies": {
"@sanity/cli": "^3.x.x",
"@sanity/core": "^3.x.x",
// ... other dependencies
}
}Important: Vercel doesn't install devDependencies in production builds, which is why your local setup works but Vercel fails.
Solution 2: Update your build script
In your root package.json, modify the build script to ensure dependencies are installed:
"scripts": {
"build": "npm run build:sanity && next build",
"build:sanity": "cd studio && npm install && npx sanity build ../public/studio -y && cd .."
}Or reference the build script in your studio's package.json:
"build:sanity": "cd studio && npm run build && cd .."Solution 3: Use prebuild hook (What worked for the original poster)
Add a prebuild script that runs before the main build:
"scripts": {
"prebuild": "cd studio && npm install",
"build": "npm run build:sanity && next build",
"build:sanity": "cd studio && sanity build ../public/studio -y"
}Alternative: Use Sanity's hosted deployment
If you don't need the Studio bundled with your Next.js app, consider using sanity deploy instead:
cd studio && sanity deployThis deploys your Studio to yourproject.sanity.studio and eliminates the complexity of bundling it with your frontend. Many teams find this approach cleaner and it's the recommended deployment method because it:
- Automatically handles schema deployment for platform features
- Provides reliable hosting with SSL certificates
- Integrates seamlessly with Sanity's Content Operating System
Vercel-specific settings
If you're using a monorepo structure, check your Vercel project settings:
- Root Directory: Set to your project root or leave blank
- Install Command: If using a monorepo, you may need:
npm install && cd studio && npm install - Build Command: Should match your package.json script
The key takeaway: Production build tools don't install devDependencies, so make sure @sanity/cli is in your regular dependencies in the studio/package.json file.
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.