Neon + Prisma + Next Middleware Runtime Issue
Hey! I am running into a tricky situation using Neon with Prisma + Next + NextAuth, which causes the following error when trying to load my application:
A Node.js API is used (process.cwd) which is not supported in the Edge Runtime.
This error is a result of:
1) The Prisma + Neon client config requires adding dotenv.config(); for the Pooler, which uses process.cwd internally
2) To configure NextAuth, I rely on the Prisma client in my Next middleware.ts , which is on the Edge Runtime.
Since the Edge Runtime doesn't support process.cwd, I run into the aforementioned error.
I have also tried simply removing dotenv since it appeared as though my env variables were being set. However, with dotenv removed I get the error Error: All attempts to open a WebSocket to connect to the database failed.
Would love to see a minimal example of how you recommend using NextAuth with Prisma / Neon. Thanks!19 Replies
metropolitan-bronze•2y ago
Are you using the Serverless driver adapter with Prisma?
conventional-tanOP•2y ago
Hey Mahmoud yes I am:
import { Pool, neonConfig } from "@neondatabase/serverless";
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient as PrismaClientWithoutExtension } from "@prisma/client";
import { stepsExtension } from "./extentsions/steps";
import dotenv from "dotenv";
import ws from "ws";
dotenv.config();
neonConfig.webSocketConstructor = ws;
const connectionString =
${process.env.DATABASE_URL};
const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);
export const prisma = new PrismaClientWithoutExtension({ adapter }).$extends(
stepsExtension()
);
type PrismaClientWithExtensions = typeof prisma;
export type PrismaClient = PrismaClientWithExtensions;
export * from "@prisma/client";metropolitan-bronze•2y ago
However, with dotenv removed I get the error Error: All attempts to open a WebSocket to connect to the database failed.Were you able to console.log the DATABASE_URL? 🤔
conventional-tanOP•2y ago
yup exactly
metropolitan-bronze•2y ago
Hmm, do you still run into an error if you remove this part?
Also, have you tried using the HTTP version of the neon Serverless driver?
@ShinyPokemon correct me if I'm wrong, but that's how it's done, no?
sensitive-blue•2y ago
This should do it. I believe there’s an issue with next.js middleware. It always compiles as if run in an edge runtime. I saw something about it on twitter recently.
This theory is corroborated by https://discord.com/channels/1176467419317940276/1237113662301274172/1237533105557082165
conventional-tanOP•2y ago
Interesting. I gave this a quick try but it seems to have lead to other issues.
I create a minimum reproducible example here: https://github.com/NicHaley/prisma-next-neon-example
You will notice that when visiting "localhost:3000" you will get the original error I mentioned. Feel free to play around with it
GitHub
GitHub - NicHaley/prisma-next-neon-example
Contribute to NicHaley/prisma-next-neon-example development by creating an account on GitHub.
sensitive-blue•2y ago
Thanks for sharing. Did you try updating the client.ts with the code I shared above ?
https://github.com/NicHaley/prisma-next-neon-example/blob/main/packages/database/src/client.ts
GitHub
prisma-next-neon-example/packages/database/src/client.ts at main · ...
Contribute to NicHaley/prisma-next-neon-example development by creating an account on GitHub.
sensitive-blue•2y ago
What “other issue” did the change cause?
conventional-tanOP•2y ago
@ShinyPokemon I updated the repository above. The error I get now is
[auth][error] AdapterError: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'create')
At this point no entirely clear if this is an issue with the prisma client, @auth/prisma-adapter, or a combination of the two 🤔ratty-blush•13mo ago
any updates on this? is there a working example?
metropolitan-bronze•13mo ago
cc @Rishi Raj Jain
continuing-cyan•13mo ago
Hey @Nich,
Here's an example of using Prisma + Next.js + Neon on Vercel Edge.
https://github.com/neondatabase/examples/tree/main/with-nextjs-prisma-edge
GitHub
examples/with-nextjs-prisma-edge at main · neondatabase/examples
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples
continuing-cyan•13mo ago
Do make sure to update your
@neondatabase/serverless to the latest.ratty-blush•13mo ago
Awesome!! I'll give it a shot, thanks!!!!
ratty-blush•13mo ago
ratty-blush•13mo ago
it works!!!!!!!
btw had to install bufferutil for it to work
continuing-cyan•12mo ago
continuing-cyan•12mo ago
oh can you share a git repo?
It should work as is on https://github.com/neondatabase/examples/tree/main/with-nextjs-prisma-edge without the additional depedencies I think. Let me know if I can help.