gotchatypescriptnextjsMajor
Edge Runtime limitations: no Node.js APIs, no TCP, no native modules
Viewed 0 times
Next.js 12.2+ for Edge Runtime
edge runtimeNode.js limitationsCloudflare Workerstcp socketsbcrypt edgePrisma edge
Error Messages
Problem
Moving a Route Handler or page to Edge Runtime fails because it uses a database client, bcrypt, or any Node.js built-in. Edge Runtime is a subset of the Web API, not Node.js.
Solution
Use Edge-compatible alternatives or move to Node.js runtime:
// Bad: Prisma/pg/mongoose don't work in Edge Runtime
export const runtime = 'edge';
import { prisma } from '@/lib/prisma'; // FAILS — uses net/tcp
// Option 1: Remove the runtime export — Node.js is default
// Option 2: Use an Edge-compatible database client
import { neon } from '@neondatabase/serverless'; // HTTP-based
const sql = neon(process.env.DATABASE_URL!);
export const runtime = 'edge';
export async function GET() {
const posts = await sql
return Response.json(posts);
}
// Edge Runtime DOES support:
// fetch, Request, Response, Headers, URL, URLSearchParams
// Web Crypto API (crypto.subtle)
// TextEncoder/TextDecoder
// ReadableStream, WritableStream
// Env variables (process.env)
// Edge Runtime does NOT support:
// Node.js built-ins: fs, path, net, tls
// Native addons (.node files)
// TCP sockets (most database drivers)
// Edge-compatible password hashing:
const encoder = new TextEncoder();
const data = encoder.encode(password + salt);
const hash = await crypto.subtle.digest('SHA-256', data);
// Bad: Prisma/pg/mongoose don't work in Edge Runtime
export const runtime = 'edge';
import { prisma } from '@/lib/prisma'; // FAILS — uses net/tcp
// Option 1: Remove the runtime export — Node.js is default
// Option 2: Use an Edge-compatible database client
import { neon } from '@neondatabase/serverless'; // HTTP-based
const sql = neon(process.env.DATABASE_URL!);
export const runtime = 'edge';
export async function GET() {
const posts = await sql
SELECT * FROM posts;return Response.json(posts);
}
// Edge Runtime DOES support:
// fetch, Request, Response, Headers, URL, URLSearchParams
// Web Crypto API (crypto.subtle)
// TextEncoder/TextDecoder
// ReadableStream, WritableStream
// Env variables (process.env)
// Edge Runtime does NOT support:
// Node.js built-ins: fs, path, net, tls
// Native addons (.node files)
// TCP sockets (most database drivers)
// Edge-compatible password hashing:
const encoder = new TextEncoder();
const data = encoder.encode(password + salt);
const hash = await crypto.subtle.digest('SHA-256', data);
Why
Edge Runtime runs on V8 isolates in CDN edge nodes (Cloudflare Workers, Vercel Edge Network). These environments don't have the full Node.js runtime — only Web APIs. This enables globally distributed low-latency execution, but restricts which code can run there.
Gotchas
- Middleware always runs in Edge Runtime — never put database calls in middleware
- bcrypt uses native code — use bcryptjs (pure JS) or Web Crypto for Edge-compatible hashing
- Prisma does NOT support Edge Runtime — use Prisma's Accelerate (HTTP proxy) for edge
- Some packages detect Edge Runtime and switch behavior — check package docs
Code Snippets
Edge-compatible HMAC signing
// Edge-compatible hashing with Web Crypto
const encoder = new TextEncoder();
const keyData = encoder.encode(secret);
const key = await crypto.subtle.importKey('raw', keyData, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
const sig = await crypto.subtle.sign('HMAC', key, encoder.encode(payload));Context
When using export const runtime = 'edge' in Route Handlers, middleware, or pages
Revisions (0)
No revisions yet.