Runtime Error on latest versions of Next, AuthJS, and Prisma when initializing prisma adapter
I'm running the most recent versions of everything (just building from scratch) as soon as I enable the prisma adapter in AuthJS I get a runtime error:
I am running with Prisma Postgres which should be Edge compatible
Error: Failed to load external module node:module: ReferenceError: Cannot access 'require' before initialization
Sonnet and Devin just make a mess of everything.
Compared line by line for my resource files (auth.ts, prisma.ts, etc...) as far as I could with https://github.com/prisma/nextjs-prisma-postgres-demo/tree/main and everything looks right
export const { handlers, signIn, signOut, auth } = NextAuth({
THIS LINE --> adapter: PrismaAdapter(prisma),
providers: [Discord],
})
GitHub
GitHub - prisma/nextjs-prisma-postgres-demo
Contribute to prisma/nextjs-prisma-postgres-demo development by creating an account on GitHub.
2 Replies
You selected the bot's instantaneous insights. Let me compile your answer right away!
The error you're encountering is related to using Prisma with Next.js and Auth.js in an Edge runtime environment. This is a common issue when trying to use Prisma in Next.js middleware or Edge functions.
Why this happens
When you use the PrismaAdapter with Auth.js in a Next.js application, it attempts to run Prisma Client in an Edge runtime environment. By default, Prisma Client isn't configured to run in Edge environments like Next.js middleware or Edge functions.
As seen in multiple GitHub issues, this error occurs because:
1. Next.js middleware always runs in the Edge runtime, even locally
2. The standard Prisma Client isn't compatible with Edge runtimes
3. When using Auth.js with Prisma, the adapter tries to access the database even in middleware contexts
Solutions
You have several options to fix this issue:
1. Use JWT sessions instead of database sessions
The simplest solution is to configure Auth.js to use JWT sessions instead of database sessions:
This approach was confirmed to work by multiple users in this GitHub issue and this one.
2. Use a driver adapter with an Edge-compatible database driver
If you need database sessions, you can use Prisma's driver adapters feature with an Edge-compatible database driver:
Make sure to add
driverAdapters
to your preview features in s
chema.prisma:
3. Separate your auth configuration
Another approach is to separate your auth configuration as suggested in the Auth.js Edge compatibility guide:
Important notes
1. The issue is specifically with running Prisma in Edge environments, not with Prisma itself
2. Even with the PostgreSQL adapter, there are known issues with the pg
driver in Edge environments as mentioned in this issue
3. For local development, you might need to connect to a hosted database rather than a local one when using Edge functions
The simplest solution is to use JWT sessions if you don't need to store session data in the database.
I hope this helps resolve your issue!
š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.(I'm not entirely sure this is certainly a Prisma or AuthJS issue)