"PrismaClient is unable to be run under the Vercel Edge Runtime."
Unanswered
Exzotic posted this in #help-forum
ExzoticOP
Hello! Im getting an issue while trying to use prisma in the
(Running locally not on vercel)
middleware.ts file. It works everywhere else just not there for some reason(Running locally not on vercel)
86 Replies
is your page a edge function
ExzoticOP
I havent defined either way so
ExzoticOP
It is nodejs
so,
export const runtime = 'edge' isn't in your code anywere?ExzoticOP
Nowhere
hmm, could any libraries be enabeling that for you...
ExzoticOP
Not that I could think off
but i don't see how that would work
ExzoticOP
"dependencies": {
"@auth/prisma-adapter": "^1.0.1",
"@prisma/client": "^5.1.1",
"@types/node": "20.4.9",
"@types/react": "18.2.20",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.14",
"bcrypt": "^5.1.0",
"eslint": "8.47.0",
"eslint-config-next": "13.4.13",
"next": "13.4.13",
"next-auth": "^4.23.0",
"postcss": "8.4.27",
"prisma": "^5.1.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-icons": "^4.10.1",
"stripe": "^13.1.0",
"tailwind-merge": "^1.14.0",
"tailwindcss": "3.3.3",
"typescript": "5.1.6",
"zod": "^3.22.1"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0"
}and submiting a bug report to said url won;t do anything
so, you said some route have this isssue
is it the same ones
ExzoticOP
Every page
ohh
thats a bit problematic
ExzoticOP
I can use prisma fine in the pages / routes, just in the middleware its causing some issues
ExzoticOP
I tried adding
export const runtime = 'nodejs'; to the middleware.ts but no luck@riský https://github.com/prisma/prisma/discussions/12602
i guess it does... ?
ExzoticOP
oh middleware doesnt affect routes either it seems
this is a little weird...
ExzoticOP
yeah i might just have to add a check in every file that i want to be secure
just a quick import of a function
ExzoticOP
especially with these admin routes dont want to risk any user being able to access them
now i see why nextauth adds this stuff to jwt token, so it doesn't need to use db for middlewire
ExzoticOP
yeah i mean i can add the admin field to the token but i dont trust the middelware now
well it just doesnt work on routes is the problem
@Exzotic yeah i mean i can add the admin field to the token but i dont trust the middelware now
its good, just as long as all you do is edge server things
sorry for not having a better answer ðŸ˜
ExzoticOP
Its all good
Original message was deleted
you could probably mark this as solved as it isn't possible so that others may see it on google, but there are so many other results, so it probably doesn't matter
as in you use the command, but it doesn't matter
ExzoticOP
ah
would you know the best way to apply "middleware" to api routes?
sure
is it adding a function that you run?
ExzoticOP
yeah i have:
But im not sure the best way to add it to each admin route
export async function requireAdmin() {
const session = await getAuthSession();
if(!session.user || !session.user.admin) return new Response(null, { status: 401 });
}But im not sure the best way to add it to each admin route
as it also needs to return the actual route function
just run requireAdmin at the start of the route
ahh
ExzoticOP
yeah but it will continue the rest
what i would do is create a wrapper
that takes in a function and either returns that function or that error
Asian black bear
I didn't read through the entire thread, but the middleware is running on Edge and you can't change it. And Prisma is not supported on Edge at all.
they are basicly deciding to add the checks to all the api routes now... as of that reason
ExzoticOP
yeah as middleware doesnt even touch routes for some reason
I think in the future I might just have to go back to express + react, its nice having it in one but it seems to have a lot of problems for me at least
i would do this
and use it like
function requireAdmin(fn: Function): Function {
return async (...args: any[]) => {
const session = await getAuthSession();
if (!session.user || !session.user.admin) {
return new Response(null, { status: 401 });
}
return await fn(...args);
};
}and use it like
export const const GET = requireAdmin(() => {
// This function can only be called by admins.
})();(i think this should work)
ExzoticOP
ah thanks, will try it
but you may need to fix the types as i didn't emember what they were
(as any would be a bit annoying)
Peterbald
@Exzotic Are you getting Vercel Edge Runtime error?
It is because you called PrismaClient in middleware.ts
It is because you called PrismaClient in middleware.ts
yeah, thats the error and as near said: https://ptb.discord.com/channels/752553802359505017/1148583270280339567/1148591301533638738
Peterbald
Like this
To avoid that error you have to use @prisma/clent/edge to config Prisma
but does that require their service thing?
Peterbald
// import { PrismaClient } from '@prisma/client';
import { PrismaClient } from '@prisma/client/edge'
import { withAccelerate } from '@prisma/extension-accelerate';
const globalForPrisma = global as unknown as { prisma: PrismaClient };
// { log: ["query"] }; // Previously housed within PrismaClient();
export const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate());
if (process.env.NODE_ENV != "production") globalForPrisma.prisma;ExzoticOP
ah well my main concern was api routes and for some reason middleware doesnt affect them
use 3 back ticks
Peterbald
And have to use another DATABASE_URL.
prisma://accelerate.prisma-data.net/?api_key=......
prisma://accelerate.prisma-data.net/?api_key=......
yeah, not a native solution - while good suggestion, it requires signing up to another thing - but they may be willing
Peterbald
If you config all of above thing, you can use API function on middleware.
I think it can be helpful @Exzotic 🙂
ExzoticOP
Ah but when I was testing middleware just wasnt ran on api routes
Peterbald
Yes you are right
have to use prisma proxy
not direct url
@Exzotic Ah but when I was testing middleware just wasnt ran on api routes
Peterbald
As I said before, it is because you call the api with prisma on middleware. Please check your code out if the calling API with prisma on middleware.
ExzoticOP
I mean prisma or not, middleware just wasnt running for api routes
Peterbald
Anyway you got "PrismaClient is unable to be run under the Vercel Edge Runtime."
Right?
that is what the og screenshot said
Peterbald
What I mean is that PrismaClient must be called on client instead of middleware
ExzoticOP
Yeah I mean I tested it without prisma just to see
and middleware is not called on api routes in general
Peterbald
Could you give me your middleware file?
yeah, are you including the api paths