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

Taught in

References