Middleware.ts - Error: The edge runtime does not support Node.js 'crypto' module. #1761

Closed
opened 2026-03-13 09:01:27 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @hp8wvvvgnj6asjm7 on GitHub (Aug 23, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Is it not possible to use auth in next middleware?

middleware.ts

import { NextResponse, NextRequest } from 'next/server'
import { auth } from '@/lib/auth'
import { headers } from 'next/headers'

 
export async function middleware(request: NextRequest) {

  const session = await auth.api.getSession({
      headers: await headers(),
  })

  console.log(session)

  if (session) {
    return NextResponse.next()
  }

  // Redirect to login page if not authenticated
  return NextResponse.redirect(new URL('/login', request.url))
}
 
export const config = {
  matcher: '/dashboard/:path*',
}

Current vs. Expected behavior

Error

⨯ Error: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
    at Object.get (.next\server\edge\chunks\_7fdb9dc5._.js:62:41)
    at <unknown> (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:722:27)
    at runModuleExecutionHooks (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:768:9)
    at instantiateModule (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:720:9)
    at getOrInstantiateModuleFromParent (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:653:12)
    at commonJsRequire (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:158:20)
    at <unknown> (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:722:27)
    at runModuleExecutionHooks (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:768:9)

What version of Better Auth are you using?

1.3.4

System info

-

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @hp8wvvvgnj6asjm7 on GitHub (Aug 23, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Is it not possible to use auth in next middleware? `middleware.ts` ``` import { NextResponse, NextRequest } from 'next/server' import { auth } from '@/lib/auth' import { headers } from 'next/headers' export async function middleware(request: NextRequest) { const session = await auth.api.getSession({ headers: await headers(), }) console.log(session) if (session) { return NextResponse.next() } // Redirect to login page if not authenticated return NextResponse.redirect(new URL('/login', request.url)) } export const config = { matcher: '/dashboard/:path*', } ``` ### Current vs. Expected behavior Error ``` ⨯ Error: The edge runtime does not support Node.js 'crypto' module. Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime at Object.get (.next\server\edge\chunks\_7fdb9dc5._.js:62:41) at <unknown> (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:722:27) at runModuleExecutionHooks (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:768:9) at instantiateModule (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:720:9) at getOrInstantiateModuleFromParent (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:653:12) at commonJsRequire (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:158:20) at <unknown> (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:722:27) at runModuleExecutionHooks (.next\server\edge\chunks\investor-www_edge-wrapper_2d672148.js:768:9) ``` ### What version of Better Auth are you using? 1.3.4 ### System info ```bash - ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the nextjs label 2026-03-13 09:01:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 23, 2025):

Hi @hp8wvvvgnj6asjm7! I'm Dosu and I’m helping the better-auth team.

Better Auth relies on Node.js core modules like crypto, which aren't available in the Next.js edge runtime. That's why you see this error when calling auth.api.getSession directly in middleware. For Next.js versions 15.1.7 and below, the recommended workaround is to fetch the session from a separate API route (like /api/auth/get-session) using a fetch library, and forward the cookies from the request headers. This avoids direct use of Node.js modules in the edge runtime. Example and details are in the Better Auth Next.js integration docs.

If you're using Next.js 15.2.0 or newer, you can opt into the Node.js runtime for middleware, which allows you to use Better Auth APIs directly—but this is experimental and may change in future Next.js releases. More info on enabling this is in the Next.js middleware runtime docs.

If you want background on why these errors persist even with telemetry disabled, see this GitHub issue.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 23, 2025): <!-- Greeting --> Hi @hp8wvvvgnj6asjm7! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Better Auth relies on Node.js core modules like `crypto`, which aren't available in the Next.js edge runtime. That's why you see this error when calling `auth.api.getSession` directly in middleware. For Next.js versions 15.1.7 and below, the recommended workaround is to fetch the session from a separate API route (like `/api/auth/get-session`) using a fetch library, and forward the cookies from the request headers. This avoids direct use of Node.js modules in the edge runtime. Example and details are in the [Better Auth Next.js integration docs](https://better-auth.com/docs/integrations/next). If you're using Next.js 15.2.0 or newer, you can opt into the Node.js runtime for middleware, which allows you to use Better Auth APIs directly—but this is experimental and may change in future Next.js releases. More info on enabling this is in the [Next.js middleware runtime docs](https://better-auth.com/docs/integrations/next). If you want background on why these errors persist even with telemetry disabled, see [this GitHub issue](https://github.com/better-auth/better-auth/issues/3973). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5ec26551-02e0-450e-9be2-8501fba147a7?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4167)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1761