Next.js · build error

'server-only' cannot be imported from a Client Component module

what it looks like
Error: You're importing a module that depends on "server-only" into a React Client Component module. This API is only available in Server Components but one of its parents is marked with "use client", so this module is also a Client Component.

What it means

A file marked with import "server-only" (on purpose, to keep secrets on the server) ended up in a client bundle. Any file a "use client" file imports becomes client code too.

Common causes

  • A client component imports a helper from lib/ that reads env secrets or the database
  • A shared utils file mixes client-safe helpers and server-only ones

How to fix it

  1. Don't import that module from the client: call it from a Server Component or Server Action instead
  2. Split the file: client-safe helpers in one module, server-only code in another
  3. Keep the import "server-only" line: it's what caught the leak

works with Next.js 16.4.3 · React 19.3last verified

prevent it · nextjs-app-router.mdc (free Cursor Rules Pack): Server-only modules (database clients, secret readers) are never imported into client components.

Prompt for Cursor or Claude Code

Next.js blocks a "server-only" module from a Client Component. Show me the import chain from the "use client" file to that module, then split or move the code so the client only gets what it needs through props or a Server Action. Keep the server-only guard.

Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Next.js error fixer.

next: skip the next one

Skip the setup. The VibeBoot SaaS Starter gives you auth, Stripe and a database already wired on Next.js 16.4.3, with AGENTS.md and .cursor/rules that keep your agent on the Next.js 16 conventions. $49, one-time.

14-day money-back guarantee · lifetime updates

$ fixed it? get the next one first

New fixes for what AI agents break, by email.

When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.

Subscribe free