Error: The edge runtime does not support Node.js 'crypto' module

This error only come up after I added middleware Searched issue on GitHub , no report about it on better-auth (or I am blind) tried to search more info appears that next-auth also have same issue as better-auth I need help here: How to bypass it or we have to not use middleware until it fixes? 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", // I make sure this is nodejs
matcher: ["/protected"], // Apply middleware to specific routes
};
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", // I make sure this is nodejs
matcher: ["/protected"], // Apply middleware to specific routes
};
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 (the Image) The error:
The edge runtime does not support Node.js 'crypto' module
The edge runtime does not support Node.js 'crypto' module
My attempt: I tried adding into config It seems like a next issue but I make a help request here Next-auth also have the sameissue: - Nextjs middleware edge runtime problem (with Mysql database with Knex package) After I try adding into config from next js docs:
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.]
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.]
Haven't tried the canary version yet
Installation | Better Auth
Learn how to configure Better Auth in your project.
MongoDB Adapter | Better Auth
Integrate Better Auth with MongoDB.
GitHub
Nextjs middleware edge runtime problem (with Mysql database with Kn...
Environment System: OS: Windows 10 10.0.19045 CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz Memory: 19.24 GB / 31.96 GB Binaries: Node: 21.1.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22....
Routing: Middleware | Next.js
Learn how to use Middleware to run code before a request is completed.
No description
1 Reply
phyziyx
phyziyx4w ago
You have to use an API request from your Middleware to the route handler.

Did you find this page helpful?