-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use Prisma client in Next.js middleware, even when deploying to Node.js #21310
Comments
@markspolakovs you should try importing the prisma client like this |
Same issue here. |
@markspolakovs |
Thanks for confirming @millsp. Is this still true if I'm self-hosting my Next app (using Docker), rather than running it on Vercel? |
Hmm, I am trying to think what we could do to workaround this. Next.js is bundling your project, and it will bundle the separate edge function via the
You may get other hurdles down the line (eg. engine or schema not being copied in the correct locations), happy to assist as needed. |
Pretty annoying. Moving the very same function call out of the middleware and it works. |
Unfortunately it looks like Next's edge runtime blocks all this trickery: I tried the
(I ran It looks like it's getting tripped up on Prisma requiring I found a feature request on the Next side to allow configuring middleware to use the Node runtime, but with no response from the Next team: vercel/next.js#38989 |
A solution for anyone else who ran into this error just trying to set up a project - I believe you can just use jwt sessions, rather than database sessions.
|
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as off-topic.
This comment was marked as off-topic.
Why would querying the DB be any different in middleware than in a server component? To the DB client, it's all just a Node.js runtime. I hope I'm wrong, but this seems like a deliberate limitation to sell some commercial feature. Which is really outside the spirit and ethos of open source (see OSI).
|
Vercel Edge Middleware is not a Node.js runtime, but the Vercel Edge Runtime. Prisma currently can not run in that environment as it does not allow talking to databases via the normal database drivers using the normal methods like TCP connections - which Prisma usually uses. |
@janpio Might be true, but that is not a relevant fact in my case since I don't use Vercel. I deploy Next.js apps as Docker containers that run in Kubernetes. The base Docker image is always |
Then you should open a bug report instead of assuming ill intent, so we can look into that, because that of course should not happen. Obviously the error message is incorrect, and we want to look into that and fix it. (I am not fully sure if that is also the case above, so an additional bug issue with information and optimally a reproduction would be very helpful.) PS: We are also currently working on, and have a private Early Access running, for support of Vercel Edge Middleware: #21394 |
IMO this issue is about this error message appearing even and preventing usage of prisma in middleware when not using Vercel in any way, not that the prisma client can't be used in the vercel edge runtime. @janpio The original reproduction from this issue should do the trick. |
The Next.js documentation has the answer to the problem:
Source https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime So even when you run your Next.js app locally via Node.js, it unfortunately still runs the middleware via the Edge middleware instead. |
Fortunately, we just released Prisma ORM version 5.11.0 which includes a preview feature for Edge Functions support for Vercel Edge Functions and Middleware (and Cloudflare Workers and Pages) in Prisma ORM 🥳
Please give it a try, and let us know how it goes! If you encounter any problems, please create a new bug report issue, or if the problem is driver adapter specific, use the feedback discussions for With that I was able to upgrade the reproduction from the original issue description to Prisma 5.11.0, and use a PostgreSQL database from Neon (or Vercel Postgres): markspolakovs/next-prisma-repro#1 (Unfortunately the Edge Runtime does not support reading local files, so using SQLite - which is a local file - is out of the question, so I had to switch to using PostgreSQL.) |
this work for me.. thanks |
Bug description
Next.js middleware seems to fail the edge runtime check, even when deployed to Node.js:
Wasn't sure if this is a Next or a Prisma bug.
How to reproduce
yarn dev
Expected behavior
DB operations to work inside middleware.
Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: