Fix guides

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.

No passwords or secret keys, please. We only use your email to reply about this.

What you are seeing

Why it happens

  1. 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.
  2. The import does not have to be direct. If a client component imports lib/supabase.ts, and that file imports next/headers anywhere at the top, the whole chain breaks.
  3. Supabase's Next.js setup uses two different clients on purpose: createBrowserClient for Client Components and createServerClient (with the cookie store) for Server Components, Server Actions, and Route Handlers. AI tools often merge them into one file "for simplicity".
  4. next/headers is 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.