PrismaClient is unable to run in this browser environment
Checklist
- [x] The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
- [x] I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
- [x] I have looked into the API documentation and have not found a suitable solution or answer.
- [x] I have searched the issues and have not found a suitable solution or answer.
- [x] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- [x] I agree to the terms within the Auth0 Code of Conduct.
Description
With this code
import { Auth0Client } from "@auth0/nextjs-auth0/server"; import {NextResponse} from "next/server"; import {PrismaClient} from "@prisma/client";
export const auth0 = new Auth0Client({ authorizationParameters: { scope: process.env.AUTH0_SCOPE, }, beforeSessionSaved: async (session) => { console.log('before oauth0 session', session); const prisma = new PrismaClient(); const roles = await prisma.userProfile.findFirst({ where: {sub: session.user.sub}, select: {roles: true} }); console.log('roles', roles); return session } });
I have an error
Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
How can I fix it?
Reproduction
Implement prisma client on beforeSessionSaved hook
Additional context
No response
nextjs-auth0 version
4.5.1
Next.js version
15.3.2
Node.js version
22.13.1
Hello @jorge-riberi ,
Looks like it is happening because beforeSessionSaved is being evaluated in an edge environment, and Prisma is not compatible with the edge runtime. Prisma requires a Node.js environment to function properly.
Most likely it's triggered by const authRes = await auth0.middleware(request); used in middleware.ts.
You can add a debug log like console.log('Runtime:', process.env.NEXT_RUNTIME); inside the beforeSessionSaved hook to check whether it's being executed in the Edge Runtime.
beforeSessionSaved: async (session) => {
console.log("Running in Edge:", process.env.NEXT_RUNTIME); // Logs 'edge' if running in the Edge Runtime
const prisma = new PrismaClient();
const roles = await prisma.userProfile.findFirst({
where: {sub: session.user.sub},
select: {roles: true}
});
console.log('roles', roles);
return session
}
Solution:
This is not a bug with our SDK, so I can’t provide a definitive fix. However, here are a few helpful pointers you can explore:
-
Next.js Middleware Runtime: Refer to the Next.js documentation on middleware runtime configuration. You can set the runtime to
nodejsusing an experimental option, though it's not the recommended solution. -
Split your Auth0 client configuration: Creating two separate
Auth0Clientinstances:- One for Edge environments, which omits any server-only logic such as
Prisma. - One for Node.js environments, where you can safely use
Prismain hooks likebeforeSessionSaved. Since I’m not familiar with the specifics of your codebase, this approach may not be suitable in all cases. Please adopt it only if it aligns with your architecture.
- One for Edge environments, which omits any server-only logic such as
Hi @nandan-bhat, thanks a lot for your response. I will follow your instructions.