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 generate after 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)

Most upvoted comments

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:

Error: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
    at Object.get (.next/server/edge/chunks/_608f43._.js:62:19)
    at [project]/node_modules/pg/lib/crypto/utils-webcrypto.js [middleware] (ecmascript) (.next/server/edge/chunks/node_modules_5b1a03._.js:5725:36)
    at <unknown> (.next/server/edge/chunks/edge-wrapper_f533d1.js:693:27)
    at runModuleExecutionHooks (.next/server/edge/chunks/edge-wrapper_f533d1.js:738:9)
    at instantiateModule (.next/server/edge/chunks/edge-wrapper_f533d1.js:691:9)
    at getOrInstantiateModuleFromParent (.next/server/edge/chunks/edge-wrapper_f533d1.js:624:12)
    at commonJsRequire (.next/server/edge/chunks/edge-wrapper_f533d1.js:157:20)
    at [project]/node_modules/pg/lib/crypto/utils.js [middleware] (ecmascript) (.next/server/edge/chunks/node_modules_5b1a03._.js:5843:22)
    at <unknown> (.next/server/edge/chunks/edge-wrapper_f533d1.js:693:27)
    at runModuleExecutionHooks (.next/server/edge/chunks/edge-wrapper_f533d1.js:738:9)

@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:

[auth][cause]: Error: PrismaClient is not configured to run in Vercel Edge Functions or Edge Middleware. In order to run Prisma Client on edge runtime, either:
- Use Prisma Accelerate: https://pris.ly/d/accelerate
- Use Driver Adapters: https://pris.ly/d/driver-adapters

If this is unexpected, please open an issue: https://pris.ly/prisma-prisma-bug-report
    at Object.get (webpack-internal:///(middleware)/./node_modules/.prisma/client/index-browser.js:223:15)
    at Object.jwt (webpack-internal:///(middleware)/./auth.config.ts:108:80)
    at Module.session (webpack-internal:///(middleware)/./node_modules/next-auth/node_modules/@auth/core/lib/actions/session.js:27:43)
    at async AuthInternal (webpack-internal:///(middleware)/./node_modules/next-auth/node_modules/@auth/core/lib/index.js:47:24)
    at async Auth (webpack-internal:///(middleware)/./node_modules/next-auth/node_modules/@auth/core/index.js:126:34)
    at async handleAuth (webpack-internal:///(middleware)/./node_modules/next-auth/lib/index.js:134:29)
    at async adapter (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/adapter.js:179:16)
    at async /workspace/NextAuth5/node_modules/next/dist/server/web/sandbox/sandbox.js:110:22
    at async runWithTaggedErrors (/workspace/NextAuth5/node_modules/next/dist/server/web/sandbox/sandbox.js:107:9)
    at async DevServer.runMiddleware (/workspace/NextAuth5/node_modules/next/dist/server/next-server.js:1062:24)

But that is expected as the project is using a Next.js middleware, which runs in the edge runtime, even locally:

Middleware currently only supports the Edge runtime. The Node.js runtime can not be used.

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… 💥

image

Unfortunately it seems that the pg database driver is not compatible with the Next.js middlewares as well. Here is an issue for that in the pg repository: https://github.com/brianc/node-postgres/issues/3206

~~There supposedly is a workaround that pg could 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 pg in 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-pg one will have to use one of the other driver adapters, for example the one for Neon, PlanetScale or Turso. Unfortunately nothing Prisma or pg can 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

image

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.

import { PrismaClient } from '@prisma/client'
import { neon } from '@neondatabase/serverless'
import { PrismaNeonHTTP } from '@prisma/adapter-neon'

const connection = neon(`${process.env.DATABASE_URL}`,{
    arrayMode:false,
    fullResults:true
})
const adapter = new PrismaNeonHTTP(connection)
const db = new PrismaClient({adapter})

export default db

Sorry, I think I understand. I will try it first.