Skip to main content
Practice Problems

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:

tsx
// 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:

tsx
// Opt-in to Edge runtime export const runtime = "edge"; export default function Page() { return <div>This runs at the edge!</div>; }
tsx
// 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

FeatureNode.js RuntimeEdge Runtime
Cold startSlower (~250ms)Very fast (~1-5ms)
LocationSingle regionEdge locations worldwide
Max execution timeNo limit (varies by host)Limited (typically 30s)
Bundle sizeNo limitLimited (~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:

tsx
// 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

tsx
// 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems