# Edge vs Node.js Runtime

> Speed vs Compatibility.

Subject: Next.js | Lesson 61 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/edge-vs-node-runtime
Previous: [Environment Variables](https://codewithanimation.in/nextjs/environment-variables.md) | Next: [Draft Mode](https://codewithanimation.in/nextjs/draft-mode.md)

## Two Server Environments

By default, Next.js executes Server Components and Route Handlers in a standard Node.js environment. This means you have full access to the filesystem (`fs`), cryptography (`crypto`), and heavy npm packages.

However, Next.js also supports the **Edge Runtime**—a stripped-down, ultra-fast V8 isolate environment (like Cloudflare Workers) that boots in milliseconds and runs globally close to the user.

## The Trade-off

The Node.js runtime suffers from 'Cold Starts' (taking 1-3 seconds to boot up on the first request) and is usually centralized in one region (e.g., US-East).

The Edge runtime has **zero cold starts** and is deployed to hundreds of locations globally. It's the only runtime allowed for Middleware because Middleware must intercept every single request instantly.

The catch? Edge is heavily restricted. It does not support native Node.js APIs.

## Opting into the Edge

You can switch any specific route or page to the Edge runtime by exporting a configuration variable.

```typescript
// app/api/fast-route/route.ts
export const runtime = 'edge';

export async function GET() {
  // This will run globally on the Edge Network.
  return Response.json({ message: 'Instant!' });
}
```

## Common mistake: The Node Module Crash

The most common pain point is trying to use a Node-dependent library in an Edge route.

If you import `bcrypt` (which relies on C++ Node bindings) or `fs` inside Middleware, Next.js will instantly throw a catastrophic error.

You must switch to Web-Standard APIs (e.g., the Web Crypto API instead of Node's `crypto`) or use Edge-compatible libraries.

## Try it: Runtime Simulator

Toggle the runtime environment and watch how importing Node native modules behaves differently.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/edge-vs-node-runtime

## Check yourself

### Question 1

Why can't you use the 'fs' (filesystem) module inside Next.js Middleware?

- A. Because Middleware runs on the browser.
- B. Because Middleware runs on the Edge Runtime, which strips out native Node.js APIs like 'fs'.
- C. Because 'fs' is deprecated in modern Node.js.
- D. Because Middleware is strictly synchronous and 'fs' is asynchronous.

Correct answer: B

Middleware exclusively uses the Edge Runtime to guarantee zero cold starts globally. The Edge Runtime does not have access to the underlying operating system's filesystem.

## Remember this

- Node.js (Default): Full compatibility, centralized region, has cold starts.
- Edge Runtime: Limited compatibility (Web APIs only), global distribution, zero cold starts.
- Middleware strictly requires the Edge Runtime.
- You can opt a Route Handler or Server Component into the Edge using `export const runtime = 'edge';`
