Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Next.js middleware breaks when it imports Node.js crypto

ErrorMiddleware or a package it imports fails to build because the Edge runtime does not support the Node.js crypto module

Short answer

Middleware runs on the Edge runtime by default, and that runtime has no native Node.js modules such as crypto. Either rewrite the code with the Web Crypto API (crypto.subtle), switch the middleware to the Node.js runtime on Next.js 15.5 or later, or move to proxy.ts on Next.js 16, which runs on Node.js.

  • Applies to Next.js 15.5, Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find what pulls crypto into the middleware bundle. Search the middleware file and the files it imports for Node-only imports.

    grep -rnE "from ['\"](node:)?(crypto|fs|path|stream|buffer)['\"]|require\(" middleware.ts src/middleware.ts lib 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The import is import crypto from 'crypto' or require('crypto') in a file the middleware loads.Your own code calls Node's crypto instead of the Web Crypto APIFix
    2. 2Your code has no crypto import, but the build trace points into node_modules (a JWT, session or password-hashing package).A package you import uses native Node.js APIsFix
    3. 3Next.js is 15.5 or newer, the code genuinely needs a Node.js module, and middleware.ts has no runtime setting.Middleware can run on Node.js from Next.js 15.5, but you did not select itFix
    4. 4Next.js is 16 or newer and the file is still middleware.ts, or the file is proxy.ts and still sets a runtime.On Next.js 16 the file should be proxy.ts, which uses Node.jsFix

What does the error look like?

  • Middleware or a package it imports fails to build because the Edge runtime does not support the Node.js crypto module
  • A JWT or password library works in a route handler but fails as soon as middleware.ts imports it

Cause 1 of 4

Your own code calls Node's crypto instead of the Web Crypto API

1Check

The Edge runtime lists crypto, CryptoKey and SubtleCrypto from the web platform as supported, and says native Node.js APIs are not. So the global crypto object works, but the node:crypto module does not.

grep -rn "createHash\|createHmac\|randomBytes" middleware.ts lib

2Fix

Replace the Node call with its Web Crypto equivalent. A SHA-256 hash, for example, becomes crypto.subtle.digest, which returns a promise, so the middleware function must be async.

// middleware.ts: no import needed, crypto is a global on the Edge runtime
async function sha256Hex(text: string) {
  const data = new TextEncoder().encode(text)
  const hash = await crypto.subtle.digest('SHA-256', data)
  return Array.from(new Uint8Array(hash))
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('')
}

3Verify

Run the build and request a route the middleware matches.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 4

A package you import uses native Node.js APIs

1Check

The docs allow node_modules in the Edge runtime only when the package is an ES module and does not use native Node.js APIs. Open the package's entry file or its README to see whether it targets Edge or the Web Crypto API.

npm ls --all 2>/dev/null | grep -iE "jsonwebtoken|bcrypt|jose"

2Fix

Use a package that is written for the Edge runtime or for Web Crypto, or keep the heavy check out of middleware and do it in a route handler or server code, where the full Node.js runtime is available.

// middleware.ts: keep only a cheap check here
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  if (!request.cookies.has('session')) {
    return NextResponse.redirect(new URL('/login', request.url))
  }
  return NextResponse.next()
}

3Verify

Rebuild; the Node-only package must no longer appear in the middleware bundle.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 4

Middleware can run on Node.js from Next.js 15.5, but you did not select it

1Check

Middleware defaults to the Edge runtime. Node.js runtime support became stable in 15.5.0 (experimental in 15.2.0). Check your version first.

npx next --version

2Fix

Set the runtime to nodejs in the config object of the middleware file.

// middleware.ts
export const config = {
  runtime: 'nodejs',
  matcher: '/dashboard/:path*',
}

3Verify

Rebuild and request a matched route; the Node-only import now loads.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 4 of 4

On Next.js 16 the file should be proxy.ts, which uses Node.js

1Check

In version 16 the middleware convention is renamed to proxy. Proxy defaults to the Node.js runtime and the runtime option is not available there; setting it throws an error. Edge is not supported in proxy, so keep middleware.ts only if you need Edge.

ls middleware.* proxy.* src/middleware.* src/proxy.* 2>/dev/null

2Fix

Run the codemod to rename the file and the exported function, then delete any runtime line from the config.

npx @next/codemod@canary middleware-to-proxy .

3Verify

Rebuild, then request a route the proxy matches.

npm run build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Treat middleware as a thin gate: read cookies, redirect, rewrite. Put hashing, database calls and token signing in route handlers or server code.
  • Before adding an auth or JWT package, check whether its docs say it supports the Edge runtime.
  • Pin the Next.js version in package.json and read the middleware or proxy page of the matching docs version when you upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.