prisma: Can't use Prisma Client in Next.js middleware with `@prisma/adapter-pg` and `pg`, even locally
Also, @CodeZak, did you re-run
prisma generateafter installing the new Prisma Client version?yeah, i did that, I have pushed a new repo that you can take a look at to investigate the issue: https://github.com/CodeZak/NextAuth5 The code runs Prisma on the middleware through the authConfig file.
_Originally posted by @CodeZak in https://github.com/prisma/prisma/issues/21310#issuecomment-2147682588_
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 8
- Comments: 19 (5 by maintainers)
I’m running the “same” issue. I need to access my db in the middleware, so I followed this guide:
But then Edge complains about pg using the crypto module:
@CodeZak I gave your reproduction a shot. Just running the obvious commands and flows (I had to log the verification link instead of using Resend, that was much easier), I indeed get this error message:
But that is expected as the project is using a Next.js middleware, which runs in the edge runtime, even locally:
via https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime
That is why the error message tells you theoption
Use Driver Adapters: https://pris.ly/d/driver-adapters.Explanation: As in edge runtime of Next.js Prisma can not talk to databases by default, we built driver adapters (currently behind preview feature flag
driverAdapters). That allows you to use a database driver that does work in the edge runtime that the Next.js middleware uses.Ok, let’s follow this:
The obvious driver adapter when using PostgreSQL is https://www.prisma.io/docs/orm/overview/databases/postgresql#using-the-node-postgres-driver, so apply the steps from there and adapt your code a bit. Run the app again and… 💥
Unfortunately it seems that the
pgdatabase driver is not compatible with the Next.js middlewares as well. Here is an issue for that in thepgrepository: https://github.com/brianc/node-postgres/issues/3206~~There supposedly is a workaround that
pgcould apply: https://github.com/brianc/node-postgres/issues/3206#issuecomment-2135313773~~So conclusion: Prisma works, but the database driver for PostgreSQL does not right now.
My code changes: https://github.com/CodeZak/NextAuth5/pull/1
The understanding in https://github.com/brianc/node-postgres/issues/3206 has evolved in that it is not possible to enable usage of
pgin the Edge Runtime (which is used by Next.js Middleware and others edge runtime functionality) because of the missing TCP socket support. This is a limitation of the Edge Runtime by Vercel that is used in Next.js and when deployed to Vercel.Instead of
@prisma/adapter-pgone will have to use one of the other driver adapters, for example the one for Neon, PlanetScale or Turso. Unfortunately nothing Prisma orpgcan do to workaround this limitation.i run into the same issue on nextjs 15 project : [Error: The edge runtime does not support Node.js ‘crypto’ module. Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime]
And to follow up to show that Prisma works fine in this context, here is the setup required to use a Neon PostgreSQL database: https://github.com/CodeZak/NextAuth5/pull/2
In my case, I was trying to implement authentication with Auth.js and was using Prisma, so I had to revert the version of
@auth/prisma-adapter to 2.7.4
And enable the feature to enable “nodeMiddleware: true”
In next.config.ts
experimental: { nodeMiddleware: true, },And in middleware file
export const config = { runtime: 'nodejs', }This worked for me
Some links that helped me NextJS, Auth.js and Postgres. Any way to make this combo work? Next.js runtime config
I let prisma client orm connect to neondb in the vercel edge runtime with the http driver adapter.
Sorry, I think I understand. I will try it first.