Edge and Node.js Runtimes

Next.js has two server runtimes where you can render parts of your application code: the Node.js Runtime and the Edge Runtime. Depending on your deployment infrastructure, both runtimes support streaming.

By default, the app directory uses the Node.js runtime, which is also the default for Serverless functions. You can also use the Edge Runtime, which is the lightweight runtime used by Middleware and Edge API Routes.

There are two ways you can switch between runtimes: Globally and per Route Segment.

Global Runtime Option

To configure the runtime for your whole application, you can set the experimental option runtime in your next.config.js file:

module.exports = {
  experimental: {
    runtime: 'experimental-edge', // 'node.js' (default) | 'experimental-edge'

This option determines which runtime should be used as the default for route segments.

Segment Runtime Option

On each route segment, you can optionally export a variable to set the 'nodejs' or 'experimental-edge' runtime:

export const runtime = 'experimental-edge'; // 'node.js' (default) | 'experimental-edge'

When both the per-segment runtime and global runtime are set, the per-segment runtime overrides the global runtime. If the per-segment runtime is not set, the global runtime option will be used.

Runtime Differences

Node (Server)Node (Serverless)Edge
Cold Boot/~250msInstant
HTTP StreamingYesYesYes
Code Size/50MB1MB
npm PackagesAllAllA smaller subset

Next Steps