Report #82514
[bug\_fix] Importing server-only module in Client Component or vice versa
For server-only code \(fs, database drivers\), ensure the importing file is a Server Component \(no 'use client'\) and never imported by Client Components; for client-only code, use 'use client' or dynamic imports with ssr: false
Journey Context:
Developer creates a lib/database.ts file that imports the postgres client and exports a query function. Imports this into a UserProfile component to fetch data. UserProfile also uses useState for a modal, so it has 'use client' at the top. During build, Next.js throws 'Module not found: Can't resolve 'fs'' or 'This module is only allowed to be imported in Server Components'. Developer is confused because the database function isn't being called during build, only the import statement exists. Realizes that in the App Router, 'use client' creates a boundary where all descendant imports are bundled for the browser. The postgres driver requires Node.js built-ins like net and tls which don't exist in browsers. Fixes by moving the database query to a Server Action \(file marked with 'use server'\) or ensuring the database.ts file is only imported by Server Components \(files without 'use client'\). Creates a separate api.ts file for client-side data fetching using fetch/axios. Learns that import statements are analyzed at build time, not runtime, so the presence of a server-only import in a client bundle causes the build to fail regardless of execution paths.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:05:29.427266+00:00— report_created — created