Edge runtime vs Node.js runtime in Next.js
Runtimes in Next.js
Next.js supports two runtimes for server-side code: the default Node.js runtime and the Edge runtime. Each has different capabilities, performance characteristics, and API availability.
Node.js Runtime (Default)
The standard Node.js environment with full access to all Node.js APIs:
// Default β no config needed
export default async function Page() {
// Full Node.js API access
const fs = require("fs");
const data = fs.readFileSync("./data.json", "utf-8");
return <div>{data}</div>;
}Edge Runtime
A lightweight runtime based on Web APIs (similar to Cloudflare Workers). Runs at CDN edge locations for minimal latency:
// Opt-in to Edge runtime
export const runtime = "edge";
export default function Page() {
return <div>This runs at the edge!</div>;
}// Edge API route
export const runtime = "edge";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const name = searchParams.get("name");
return Response.json({ hello: name });
}Comparison
| Feature | Node.js Runtime | Edge Runtime |
|---|---|---|
| Cold start | Slower (~250ms) | Very fast (~1-5ms) |
| Location | Single region | Edge locations worldwide |
| Max execution time | No limit (varies by host) | Limited (typically 30s) |
| Bundle size | No limit | Limited (~1-4MB) |
| Node.js APIs | β Full access | β Limited subset |
| npm packages | β All | β οΈ Only Web API-compatible |
| File system | β
fs module | β Not available |
| Native modules | β Yes | β No |
| Streaming | β Yes | β Yes |
| Web APIs | β Yes | β Yes (native) |
Middleware (Always Edge)
Next.js Middleware always runs on the Edge runtime:
// middleware.ts β automatically runs on Edge
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// Fast execution at edge location closest to user
const country = request.geo?.country || "US";
if (country === "UA") {
return NextResponse.redirect(new URL("/ua", request.url));
}
return NextResponse.next();
}When to Use Each
Use Node.js Runtime (default)
- Database connections (Prisma, Drizzle with native drivers)
- File system operations
- Heavy computation
- Complex npm packages
- Long-running operations
Use Edge Runtime
- Personalization (geo, A/B testing)
- Redirects and rewrites
- Authentication token verification
- Simple API responses
- When latency matters most
Setting Runtime
// Per-page or per-route
export const runtime = "edge"; // or "nodejs" (default)
// In route handlers
export const runtime = "edge";
export async function GET() {
return Response.json({ time: Date.now() });
}Important:
The Node.js runtime is the default and supports the full Node.js ecosystem. The Edge runtime is for performance-critical paths that need minimal latency and run close to users. Use Edge for middleware, simple APIs, and personalization. Use Node.js for database access, file operations, and complex logic.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.