[GH-ISSUE #1973] Error: The edge runtime does not support Node.js 'crypto' module #17621

Closed
opened 2026-04-15 15:48:16 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Benn-Lau on GitHub (Mar 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1973

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

What I did:

  1. I create new next js app to try out better auth
  2. I follow the installation guide here
  3. I install mongo db adapter guide here
  4. I copy and modify middleware.ts to work with my directory
  5. bug ->This error occurs
    Image

My middleware.ts

import { NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";
import { auth } from "@/utils/auth";

export async function middleware(request: NextRequest) {
  const session = await auth.api.getSession({
    headers: await headers(),
  });

  if (!session) {
    return NextResponse.redirect(new URL("/", request.url));
  }

  return NextResponse.next();
}

export const config = {
  runtime: "nodejs",
  matcher: ["/protected"], // Apply middleware to specific routes
};

Current vs. Expected behavior

No error and middlewar.ts don't cause any issue when redirect back to /

What version of Better Auth are you using?

1.2.4

Provide environment information

My Nextjs version: "15.2.3"


{
  "name": "learn-better-auth",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "better-auth": "^1.2.4",
    "class-variance-authority": "^0.7.1",
    "clsx": "^2.1.1",
    "lucide-react": "^0.483.0",
    "mongodb": "^6.15.0",
    "next": "15.2.3",
    "react": "^16.8 || ^17.0 || ^18.0 || ^19.0",
    "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0",
    "tailwind-merge": "^3.0.2",
    "tw-animate-css": "^1.2.4"
  },
  "devDependencies": {
    "@tailwindcss/postcss": "^4",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "tailwindcss": "^4",
    "typescript": "^5"
  }
}

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

Package

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { clientPromise } from "./mongodb";
import { nextCookies } from "better-auth/next-js";

const client = await clientPromise;
const db = client.db("auth");

export const auth = betterAuth({
  database: mongodbAdapter(db),
  emailAndPassword: { enabled: true },
  plugins: [nextCookies()],
});

Additional context

My attempt:

I tried adding into next.config.ts but it cause error

  experimental: {
    nodeMiddleware: true,
  },

It seems like a next js issue but I make a pull request here anyways

Next-auth also have the same issue:

The error:

node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^

[CanaryOnlyError: The experimental feature "experimental.nodeMiddleware" can only be enabled when using the latest canary version of Next.js.]
Originally created by @Benn-Lau on GitHub (Mar 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1973 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ## What I did: 1. I create new next js app to try out better auth 2. I follow the installation guide [here](https://www.better-auth.com/docs/installation) 3. I install mongo db adapter guide [here](https://www.better-auth.com/docs/adapters/mongo) 4. I copy and modify `middleware.ts` to work with my directory 5. bug ->This error occurs <br/> ![Image](https://github.com/user-attachments/assets/257ed791-9463-41da-bbc6-b1c1c5a96e17) ## My middleware.ts ``` import { NextRequest, NextResponse } from "next/server"; import { headers } from "next/headers"; import { auth } from "@/utils/auth"; export async function middleware(request: NextRequest) { const session = await auth.api.getSession({ headers: await headers(), }); if (!session) { return NextResponse.redirect(new URL("/", request.url)); } return NextResponse.next(); } export const config = { runtime: "nodejs", matcher: ["/protected"], // Apply middleware to specific routes }; ``` ### Current vs. Expected behavior No error and `middlewar.ts` don't cause any issue when redirect back to `/` ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash My Nextjs version: "15.2.3" { "name": "learn-better-auth", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "better-auth": "^1.2.4", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "lucide-react": "^0.483.0", "mongodb": "^6.15.0", "next": "15.2.3", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0", "tailwind-merge": "^3.0.2", "tw-animate-css": "^1.2.4" }, "devDependencies": { "@tailwindcss/postcss": "^4", "@types/node": "^20", "@types/react": "^19", "@types/react-dom": "^19", "tailwindcss": "^4", "typescript": "^5" } } ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { clientPromise } from "./mongodb"; import { nextCookies } from "better-auth/next-js"; const client = await clientPromise; const db = client.db("auth"); export const auth = betterAuth({ database: mongodbAdapter(db), emailAndPassword: { enabled: true }, plugins: [nextCookies()], }); ``` ### Additional context ## My attempt: I tried [adding into next.config.ts](https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime) but it cause error ``` experimental: { nodeMiddleware: true, }, ``` It seems like a next js issue but I make a pull request here anyways Next-auth also have the same issue: - [Nextjs middleware edge runtime problem (with Mysql database with Knex package)](https://github.com/nextauthjs/next-auth/issues/11595#top) #11595 ## The error: ``` node:internal/process/promises:394 triggerUncaughtException(err, true /* fromPromise */); ^ [CanaryOnlyError: The experimental feature "experimental.nodeMiddleware" can only be enabled when using the latest canary version of Next.js.] ```
GiteaMirror added the lockedbug labels 2026-04-15 15:48:16 -05:00
Author
Owner

@ricardo-a-alves-alb commented on GitHub (Mar 24, 2025):

You have to be using next@canary to be able to use nodejs as the middleware runtime.

Related Next.js Documentation

Note: This feature is not yet recommended for production use. Therefore, Next.js will throw an error unless you are using the next@canary release instead of the stable release.

<!-- gh-comment-id:2747735963 --> @ricardo-a-alves-alb commented on GitHub (Mar 24, 2025): You have to be using `next@canary` to be able to use `nodejs` as the middleware runtime. [Related Next.js Documentation](https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime) > Note: This feature is not yet recommended for production use. Therefore, Next.js will throw an error unless you are using the next@canary release instead of the stable release.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17621