Sanity deployment to Vercel failing with multiple errors
A build that works locally but fails on Vercel usually means the page reads data that is undefined at build time. Common reasons: the Sanity project ID or dataset isn't set in Vercel, the posts aren't published, or the code doesn't handle a null result. A few setup issues come up often too: Node version, the build script, the root directory and CORS origins.
1. Set the environment variables in Vercel
Vercel doesn't read your local .env.local. In the Vercel project go to Settings → Environment Variables and add every value from .env.local, for example NEXT_PUBLIC_SANITY_PROJECT_ID and NEXT_PUBLIC_SANITY_DATASET. Then redeploy (Deploy to Vercel). Your project ID is in sanity.config.ts, or run npx sanity manage from the Studio folder.
You can also use Sanity's Vercel integration. It adds NEXT_PUBLIC_SANITY_PROJECT_ID, NEXT_PUBLIC_SANITY_DATASET and read/write token variables to the Vercel project for you. Keep the tokens server-side only.
2. Make sure the data exists and guard against null
- Only published documents come back from the public API. Drafts don't.
- A
[0]GROQ query returnsnullwhen nothing matches the slug. If you destructure it (const { title } = post), the build crashes. The current tutorial uses null-safe access:
const post = await client.fetch(POST_QUERY, { slug });
// ...
<h1>{post?.title}</h1>3. Use the current tutorial code
The tutorial was updated on March 16, 2026. It now uses the App Router with server components and next-sanity, which includes the client, PortableText and defineQuery in one package. If your code imports groq and @portabletext/react separately and uses the Pages Router, it follows the older version. The current client setup:
npm install next-sanity @sanity/image-url// src/sanity/client.ts
import { createClient } from "next-sanity";
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
apiVersion: "2025-05-01",
useCdn: false,
});4. Check the build settings
- Framework Preset: set it to Next.js.
- Root Directory: if your repo has separate
studio/andfrontend/folders, as the tutorial does, point Vercel atfrontend. - Turbopack: if the deploy fails, try removing
--turbopackfrom thebuildscript inpackage.json. The Sanity course notes this is probably a temporary issue (source). - Node.js: if the same repo builds the Studio, note that Sanity Studio v6 needs Node.js 22.12 or later (Installing Node.js). You can declare it with:
"engines": {
"node": ">=22.12.0"
}5. Add CORS origins where needed
Server-side fetches (server components, SSR, build time) don't need a CORS origin. You need one when the browser requests Sanity content directly, or when a Studio is hosted on your Vercel domain. Add your production URL, for example https://your-project.vercel.app, in either of two ways (CORS docs):
- Console: sanity.io/manage → your project → Settings → API settings → CORS Origins → Add CORS origin.
- CLI, from the Studio folder:
npx sanity cors add https://your-project.vercel.appTurn on Allow credentials only for origins that host a Studio or send authenticated requests. Don't add a credentialed https://*.vercel.app wildcard. For preview deployments, scope the wildcard to your team slug instead, for example https://*-my-team.vercel.app.
If it still fails, open the Vercel Build Logs and find the first error. That's usually the real cause, and the errors after it are side effects.
Show original thread15 replies
Was this answer helpful?
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.