Next.js + Supabase build fails: "You're importing a component that needs next/headers"
Your Cursor, Claude Code, or v0 Next.js app runs in dev but the Vercel build stops with a next/headers error, almost always because one Supabase helper file is shared by server and browser code. How to split it into a browser client and a server client, and stop it coming back.
Free · no card
Paste your error. Get a free diagnosis by email.
Tell us what broke and we’ll email you a short diagnosis: the likely cause and what to try first. Usually within a day.
What you are seeing
next build(locally or on Vercel) fails withYou're importing a component that needs "next/headers". That only works in a Server Component, followed by an import trace that ends in something likelib/supabase.tsorutils/supabase.ts.- The trace runs through a file that starts with
'use client'(a login form, navbar, or dashboard component). - It started right after your AI tool "added auth" or "fixed sessions", often by adding
import { cookies } from 'next/headers'to the one Supabase file the whole app imports. - Variants of the same problem:
cookies() was called outside a request scope, or the error mentions thepages/directory because a Pages Router page imports the helper.
Why it happens
- Next.js App Router builds two module graphs. A file marked
'use client', and everything it imports, goes into the browser bundle.next/headers(cookies(),headers()) only exists on the server, so the build refuses to bundle it for the browser. - The import does not have to be direct. If a client component imports
lib/supabase.ts, and that file importsnext/headersanywhere at the top, the whole chain breaks. - Supabase's Next.js setup uses two different clients on purpose:
createBrowserClientfor Client Components andcreateServerClient(with the cookie store) for Server Components, Server Actions, and Route Handlers. AI tools often merge them into one file "for simplicity". next/headersis also not supported in the Pages Router (pages/), so a mixed app that imports the server helper from a page hits a similar error.
How to fix it
1. Read the import trace to find the chain
# Run the build locally so you see the full trace
npm run build
# The trace lists files from the next/headers import up to the
# 'use client' file. Note both ends, e.g.:
# ./lib/supabase.ts <- imports next/headers
# ./components/LoginForm.tsx <- 'use client'
# Find every file that imports next/headers
grep -rn "next/headers" --include=*.ts --include=*.tsx --exclude-dir=node_modules .
2. Create a browser-only client
// lib/supabase/client.ts (no next/headers here, ever)
import { createBrowserClient } from '@supabase/ssr'
export function createClient() {
return createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
)
}
3. Create a server-only client
// lib/supabase/server.ts
import 'server-only' // npm install server-only
import { createServerClient } from '@supabase/ssr'
import { cookies } from 'next/headers'
export async function createClient() {
const cookieStore = await cookies()
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
return cookieStore.getAll()
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options))
} catch {
// called from a Server Component; the proxy/middleware refreshes sessions
}
},
},
},
)
}
4. Point each file at the right client
// In any file that starts with 'use client':
import { createClient } from '@/lib/supabase/client'
const supabase = createClient()
// In Server Components, Server Actions ("use server"), and route.ts handlers:
import { createClient } from '@/lib/supabase/server'
const supabase = await createClient()
// Then delete the old combined lib/supabase.ts so nothing imports it by accident.
5. Pass data down instead of importing server code
If a client component needs the signed-in user or some rows, fetch them in the parent Server Component (page.tsx or layout.tsx) with the server client, and pass plain values to the client component as props. Do not move the cookie code into the client component, and do not remove 'use client' from a component that uses useState, useEffect, or onClick just to make the build pass.
6. Keep the session refresh in middleware or proxy
Supabase's guide uses a middleware.ts file (renamed proxy.ts in Next.js 16; on Next.js 15 and earlier a proxy.ts file is never called) to refresh the auth token on each request. If your AI tool deleted or renamed it while fixing the build, put it back, or users will be signed out at random even though the build now passes.
How to know it is fixed
npm run build finishes locally, the Vercel deployment goes green, grep -rn "next/headers" only matches server files (lib/supabase/server.ts, server actions, route handlers, middleware), and signing in, reloading, and loading data all work on the deployed URL. Importing lib/supabase/server.ts from a client component now fails with a clear server-only error instead of a confusing trace. Sources: Supabase: Creating a Supabase client for SSR (Next.js), Next.js with-supabase example: lib/supabase/client.ts and server.ts, Next.js: Server and Client Components, and Next.js: cookies(). Related: Vercel deploy failed with a build error and Environment variables undefined after deploy.