Skip to content

Edge & Node.js Runtimes

Think of two types of shops:

  • Edge Runtime = A vending machine. Small, fast, everywhere — but only does simple tasks (take money, give product).
  • Node.js Runtime = A full supermarket. Big, slower, located further away — but can do everything (fresh produce, deli counter, bakery).

The Edge Runtime runs your code at CDN edge nodes — hundreds of locations worldwide, very close to users. It uses a lightweight V8 isolate (not full Node.js).

app/api/geo/route.ts
export const runtime = "edge"; // 👈 Run at the edge
export async function GET(request: Request) {
const { geo } = request as any;
return Response.json({
country: geo?.country ?? "Unknown",
city: geo?.city ?? "Unknown",
latency: "sub-10ms",
});
}
  • Middleware (always runs on Edge)
  • Route handlers with export const runtime = "edge"
  • Some page rendering (limited)

FeatureEdge RuntimeNode.js Runtime
LocationCDN edge nodes globallySingle server region
Cold start~0ms (always warm)100ms–500ms
LatencySub-10ms globally50–200ms
Memory limit128MBNo practical limit
Node.js APIs❌ Not available✅ Full access
File system❌ No fs access✅ Full access
npm packagesLimited (no native bindings)All packages
Database direct❌ Use HTTP clients✅ Direct connections
Best forAuth, redirects, A/B testsComplex logic, DB access

flowchart TB
Q{"What does your\ncode need to do?"}
Q -->|"Simple checks\\n(auth, redirects, geo)\"| Edge["⚡ Edge Runtime\nFast, global, lightweight"]
Q -->|"Complex work\\n(DB queries, file ops)\"| Node["🖥️ Node.js Runtime\nFull power, no limits"]
Edge --> EM["Middleware\nURL rewrites\nAuth checks\nGeo redirects"]
Edge --> ER["Edge API Routes\nGeo lookups\nQuick transformations"]
Node --> NH["Heavy API Routes\nDatabase operations\nFile processing"]
Node --> NP["Pages with DB data\nComplex server logic"]
style Q fill:#f59e0b,color:#000
style Edge fill:#7c3aed,color:#fff
style Node fill:#059669,color:#fff
style EM fill:#9333ea,color:#fff
style ER fill:#9333ea,color:#fff
style NH fill:#4f46e5,color:#fff
style NP fill:#4f46e5,color:#fff

You cannot use these in Edge Runtime:

❌ Not Available✅ Alternative
fs (file system)Fetch from an API instead
path moduleUse string methods / URL API
crypto (Node.js)Use Web Crypto API (crypto.subtle)
bcryptHash in API routes, not middleware
Prisma / DB connectionsUse cached tokens or HTTP client
Most Node.js-specific npm packagesUse Edge-compatible alternatives

Middleware bundles must be under 1MB. Heavy libraries will fail the build.


MistakeFix
Using fs in Edge RuntimeMove file operations to Node.js route handler
Database calls in middlewareUse JWT verification (fast, no DB) instead
Importing heavy packages in middlewareKeep middleware lean — it runs on every request
Setting runtime: "edge" on a page with DB accessPages with DB queries need Node.js runtime

  • Edge Runtime = fast, runs on CDN nodes worldwide, but limited capabilities (no file system, no DB)
  • Node.js Runtime = full server capabilities, runs in one region, but more powerful
  • Middleware always runs on Edge — keep it fast and lightweight
  • Use Edge for auth checks, redirects, geo-detection; use Node.js for DB queries, file operations, heavy computations
  • If you’re not sure, start with Node.js — it can do everything Edge can, just slower globally