Server-Only Code: Import Anything
What you'll learn: How Server Components let you safely import server-only libraries without worrying about bundle size or security leaks.
The Freedom of Server-Only Execution
Remember how Server Components run once on the server and never reach the browser? This creates a powerful freedom: you can import absolutely anything that exists in your Node.js environment.
Need to read from the file system? import fs from 'fs' directly. Want to query a database? import { db } from './db-connection' without hesitation. Processing images with a heavy library? Go ahead.
Why This Changes Everything
In traditional React, every import you write gets bundled and sent to the user's browser. This meant:
- Avoiding heavy server libraries to keep bundles small
- Never importing database clients (security risk!)
- Creating API routes as a "safe layer" between frontend and backend code
Server Components eliminate this constraint. Since the component never becomes JavaScript sent to the browser, its imports never get bundled. It's just server code, period.
The Mental Model
Think of Server Components like traditional backend code (PHP, Ruby, Python) that generates HTML. You wouldn't worry about importing a database client in a Python script—it runs on the server, end of story. Server Components work the same way.
import { db } from '@/lib/database'
import { readFile } from 'fs/promises'
import { processImage } from 'heavy-image-lib'
async function ServerComponent() {
const data = await db.query('SELECT * FROM users')
// None of these imports reach the client bundle
}
Key Takeaway: Server Components can directly import database clients, file system modules, and any Node.js APIs without bundle bloat or security concerns—they're truly server-only code.