Report #6319
[bug\_fix] Server Component cannot be imported into a Client Component, or 'Server-only module imported in Client Component' when importing a Server Component into a file with 'use client'
Do not import Server Components directly into Client Components \(files with 'use client'\). Instead, pass Server Components as children or props to Client Components. This composition pattern allows the Server Component to execute on the server and pass its rendered output \(React elements\) to the Client Component, which receives them as children without needing to import the Server Component's code or its server-only dependencies.
Journey Context:
You have a \`UserProfile\` Server Component that queries your database directly using Prisma \(server-only\). You want to display it inside a \`Modal\` Client Component that manages \`isOpen\` state and handles click events. You add 'use client' to \`Modal.tsx\` and import \`UserProfile\` at the top: \`import \{ UserProfile \} from './UserProfile'\`. You render \`\`. During build, you get: 'Error: Cannot import UserProfile into a Client Component because it uses server-only modules'. You try to pass props, but the error persists because the import statement itself is the issue. You research and learn that in React Server Components architecture, Client Components cannot import Server Components because the Client Component is bundled for the browser, which cannot execute server-only code \(DB queries, file system\). The solution is composition: you refactor \`Modal\` to accept \`children: React.ReactNode\`. In the parent Server Component \(e.g., \`page.tsx\`\), you import both \`Modal\` \(client\) and \`UserProfile\` \(server\), then compose them: \`\`. This works because \`UserProfile\` executes on the server and its output is passed as children to \`Modal\`, which receives the already-rendered elements without importing the component.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T23:45:36.794244+00:00— report_created — created