Next.jsMedium
What is the difference between Node.js and Edge runtime?
The Node.js runtime is a full Node.js environment with every Node API and npm package; it is the default and recommended runtime for all Next.js code. The Edge runtime is a restricted, Web-standard environment (V8 isolates) with faster cold starts but no file system, no native modules and smaller size limits. It is deprecated for routes in current Next.js.
Node.js runtime
- Full API surface:
fs,crypto,net, streams, native addons. - Works with any npm package, ORMs and database drivers.
- Supports ISR,
"use cache", Cache Components,after()and all features. proxy.tsalways runs on Node.js since Next.js 16.
Edge runtime
- Web APIs only:
fetch,Request,Response, Web Crypto, streams,URL,TextEncoder, plusAsyncLocalStorage. - No
fs, norequire, noevalornew Function, no native modules. - Very fast startup and low memory, can run in many regions.
- No ISR; code size is limited (a few MB depending on the host).
- Status: deprecated as a route segment option; still available for the deprecated
middleware.ts.
Choosing today
- Default to Node.js for pages, Route Handlers and Proxy.
- If latency matters, prerender into the static shell (served by the CDN) and stream the dynamic parts, and run functions near the database.
- Use the Edge runtime only for legacy middleware that relies on it, and plan to migrate.
Interview point
Latency is usually dominated by data access, not cold starts. Running compute at the edge but querying a database in one region can make things slower.
Code example
tsx
1// app/api/hash/route.ts - Node.js runtime (default)2import { createHash } from "node:crypto";3import { readFile } from "node:fs/promises";45export async function GET() {6 const file = await readFile(process.cwd() + "/package.json"); // fs: Node.js only7 const sha = createHash("sha256").update(file).digest("hex");8 return Response.json({ sha });9}1011// Web-standard code runs on both runtimes:12// const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode("hi"));1314// Legacy Edge route (deprecated):15// export const runtime = "edge";
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.