server-only
A small npm package that makes the build fail if a module containing it is imported into client code.
Without it, one wrong import can bundle database credentials or private logic into JavaScript that anyone can download.
In more detail
Add import 'server-only' at the top of files that hold database access or secrets. If anyone later imports them from a Client Component, the build stops with an error instead of shipping that code to browsers.
Example
import 'server-only';
export const db = createClient(process.env.DATABASE_URL);Related terms
- secret — Any value that grants access, such as a password, access token or private key, and must be kept out of code and version control.
- Client Component — A component whose code is also sent to the browser so it can use state, effects and event handlers.
- module — A file of code whose functions, classes and variables other files can use by importing it.
Taught in
- App Router Architecture: Server-First Paradigms — Next.js 15 & Modern App Router Architecture
- Component Tree Composition: Interleaving RSC & Client — Next.js 15 & Modern App Router Architecture
- Serialization Boundaries: Props & Server Guardrails — Next.js 15 & Modern App Router Architecture