Trouble deploying to Vercel with Sanity build command

13 replies
Last updated: Jul 14, 2021
I cannot deploy to Vercel with this command.

  "scripts": {
    "build": "npm run build:sanity && next build",
    "build:sanity": "echo 'Building Sanity to public/studio' && cd studio && npx sanity build ../public/studio -y && cd .."
  },
its throwing error:

/bin/sh: sanity: command not found
Command failed with exit code 127.
I have also tried with 
npx @sanity/cli build
yarn build
yarn && sanity build
 and many more combinations. But none of them worked. However, the same command works locally.
I have then tried with Lerna, then getting:
lerna ERR! npm ci exited 1 error Command failed with exit code 1.

Any help?
AI Update

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:

  1. The CLI needs to be in your dependencies (not devDependencies)
  2. Vercel needs to install dependencies in your /studio folder
  3. The cd studio command might not be executing in the context you expect

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 deploy

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

Do you have
@sanity/cli
as a dependency in your
packages.json
?
yes, I have it as dev dependencies for next.js root folder as well.
it sometime throws "some commands not available until sanity install"
btw, the sanity project is in the /studio folder
Fwiw, I have
@sanity/cli
as a normal, non-dev dependency in my studio’s
packages.json
and run
sanity build
on netlify with no issue
It is non-dev, because it is essential to have the cli available in production to do the actual building
I guess its because I'm installing next.js in the root then sanity in /studio folder. And its vercel.
Also, if we are using
npx
then we don't need it to be in package.json right?
Locally, I guess you could use npx to run anything inside the
node_modules
folder, but for your setup to work, I guess you need to make sure that whatever you need to run (in this case the sanity cli) is also available on the build server (in this case Vercel).
Vercel doesn’t know what’s inside your local node_modules folder, but will install everything in your
packages.json
before building your site.
My
studio/packages.json
looks something like this:
{
  "name": "studio-project",
  "scripts": {
    "start": "sanity start",
    "build": "sanity build"
  },
  "dependencies": {
    "@sanity/cli": "^2.11.0",
    .....
  },
  "devDependencies": {
    "babel-eslint": "^10.1.0",
    .....
  }
}
I guess you need to make sure that
@sanity/cli
is available in the
packages.json
as a non-dev wherever your trying to use the command
sanity build
yes, still showing error. @core isalready there in the package.
I guess the issue is vercel is not taking the command of
cd studio
somehow. its trying to run from root.

@sanity/core not installed in current project
18:58:04.018  	Project-specific commands not available until you run `sanity install`
18:58:04.023  	Error: Command "build" is not available outside of a Sanity project context.
18:58:04.023  	Run the command again within a Sanity project directory, where "@sanity/core"
you might need to do npm install inside of the studio directory, i.e
cd studio && npm i && sanity build
What I would try:1. make sure
@sanity/cli
in your
studio/packages.json
as a normal dependency2. Make sure you have a “build command” in
studio/packages.json
3. In your project root
packages.json
, the build:sanity command could cd into the studio and run the studio’s build command
"build:sanity": "cd studio && npm run build"

Thanks man, I finally tried with
"prebuild"
and it worked.
Yes, I also made sure to install
yarn
before that. Got error with another devDependancy. So I guess you are right. devdependency doesn't work in Vercel.
Thanks again.

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?