The edge runtime does not support Node.js 'crypto' module
The errorError: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
Code that runs on the Edge runtime imports something built for Node.js (often jsonwebtoken, bcrypt or a database driver). Run that code on the Node.js runtime, or replace the import with a Web Crypto based one.
Why it happens
Next.js can run middleware and routes on a lightweight Edge runtime. It doesn't have Node.js APIs such as crypto, fs or child_process. Any package that uses them breaks there, even if it only imports them indirectly.
Next.js changed the defaults recently. In Next.js 15, middleware.ts defaults to Edge, and the Node.js runtime for middleware became stable in 15.5. In Next.js 16, middleware is renamed to proxy.ts, which runs on Node.js by default.
Fix it by hand
- Find the import. Run
next dev: Next.js shows in the console and the browser which file imports the unsupported module. - On Next.js 16, rename middleware to proxy with the official codemod. Proxy runs on Node.js, and setting a
runtimeoption in it throws an error, so remove any you had:npx @next/codemod@canary middleware-to-proxy . - On Next.js 15.5 or later, keep
middleware.tsand switch it to Node.js:export const config = { runtime: 'nodejs', matcher: ['/dashboard/:path*'], } - In a route that sets
export const runtime = 'edge', remove that line unless you need Edge. Routes run on Node.js by default. - If the code must stay on Edge, replace the Node import. Use the Web Crypto API (
crypto.subtle,crypto.getRandomValues) instead ofnode:crypto, and a JWT library built for Web-interoperable runtimes, such asjose, instead ofjsonwebtoken. - Run
next buildlocally and confirm the error is gone before you redeploy.
Stop it happening again
- Keep middleware or proxy small: read a cookie, redirect or rewrite. Do database and password work in server code.
- Before adding a package to middleware or an edge route, check that it supports Web APIs only.
- Run
next buildbefore every push. The dev server doesn't catch every runtime problem.
Broke After Deploy $9
Broke After Deploy flags Node-only packages and APIs used in middleware or edge routes, and runs your production build to catch what slips through.
- Checks your project for 37 known causes of "works locally, breaks in production" and runs your production build.
- Paste the error and it finds the cause in your code, then lists what will break next.
- Verdict first. Every finding shows the file and line, what it breaks, and the fix as a diff that is applied only with your OK.
Broke After Deploy: how it works, with examples · Install guide
Works with Claude Code, Claude.ai, Cursor, Codex and other agents that read SKILL.md files. No installs, no accounts, works offline. No-questions refund.