prisma: PrismaClient is unable to be run in the browser.
Bug description
I’m getting an Error: PrismaClient is unable to be run in the browser.

How to reproduce
- Create new Node Project with
npm init -y - install dependencies
npm i next react react-dom - install prisma with
npm i -D primsaand thennpm i @prisma/client - init prisma with
npx primsa init - change schema to :
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author String?
}
- push schema to postgresql with
npx prisma db push --preview-feature - create index.js like:
import {PrismaClient} from "@prisma/client"
export {PrismaClient} from "@prisma/client"
const prisma = new PrismaClient()
export default function Page(){
return <div>Hello World</div>
}
- run server
- getting error
Expected behavior
Displaying Hello World but getting an error
Prisma information
Environment & setup
- OS: Ubuntu 20.4
- Database: PostgreSQL on Heroku
- Node.js version: v10.19.0
- Prisma version:2.19.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 35 (2 by maintainers)
Commits related to this issue
- fix(database): prevent "PrismaClient is unable to be run in the browser" error See https://github.com/prisma/prisma/issues/6219 — committed to e-Learning-by-SSE/nm-self-learning by klingebiel 2 years ago
- Fix Error about 'PrismaClient' PrismaClient is unable to be run in the browser. https://github.com/prisma/prisma/issues/6219 — committed to chaevid/nextjs-carrot-market by chaevid a year ago
- feat: initiate Prisma client according to the env according to this issue: https://github.com/prisma/prisma/issues/6219 we should differentiate how we initializing the Prisma client instance — committed to incendio-ideas/incendio-project by patchwork-body a year ago
I was having a similar issue. I think a solution is to only initialize the client once and on the server only.
@fugohan You are trying to use Prisma on frontend which is not possible. Either use it in next js api routes: https://nextjs.org/docs/api-routes/introduction or use it in getServerSideProps or getStaticProps: https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering
Thought I’d chime in as my solution is different. Essentially the above sugggestions were breaking my tests. Jest seemed unable to infer that the prisma var declared and the let were the same variable resulting in a
Duplicate declaration prismaerror. I also wanted to be able to export the variable itself rather than export default to keep import type-checking. I had to update the declaration and was able to remove all the typescript and eslint exceptions:Have you somehow fix this ? 😃
This was necessary for me for me in Next.js:
You call the
PrismaClient()intogetStaticProps, and your error is solved. When you call thePrismaClient()component, you face an error.I encountered this error in my Nextjs ts project and was puzzled. Then I saw that I was exporting a function that I was using on the client from my custom ‘utilities’ directory, and from the same ‘utilities’ directory I was exporting the prisma client. So I moved the prisma client ( and other serverside modules ) to a custom ‘services’ directory. After that the error went away. (anecdotal/mayhelpsomeone).
I was able to accomplish this similarly by creating a
global.d.tsfile withAnd using the same initialization.
However, I would get the error…
I adjusted the code to initialize the variable to null, but that caused typing errors elsewhere.
Doing this led to
TS2531: Object is possible 'null'warnings in my API code (in my case NextJS API route)I ended up adding a
@ts-ignoreto theexport default prisma;line. I’m not sure if that’s really the right way to do it, but I wanted to share in case someone like me encountered Typescript errors following this approach and wanted to resolve them.Facing same issue, with the help of the suggestions above… and the new docs I found here this is my /lib/prisma.ts in next.js
Example usage:
import prisma from '../lib/prisma'I had the
experimental-edgeruntime enabled in my Next.js config (resulting in the “unable to be run in the browser error”). None of the above solutions worked for me until I disabled that. At the time of writing I’m using the latest version ofnext(12.3.1) and@prisma/client(4.4.0). I did not need to use dynamic imports or atypeof windowguard.I didn’t know that I needed extra setup to get
@prisma/clientworking with Next.js’s edge runtime. After going through the instructions at https://www.prisma.io/docs/data-platform/data-proxy, things are working now with theexperimental-edgeruntime enabled 🥳 But I still had to make sure I didn’t havefallbackNodePolyfills: falseset as well.Trying to use Prisma in edge functions without the data proxy (or being able to use both versions in the same app) doesn’t yet seem to be supported: https://github.com/prisma/prisma/issues/9928.
another possible solution without
//@ts-ignore:src: https://github.com/2color/next-prisma-data-proxy/blob/main/lib/prisma.ts PS: I also think that u should also either use getStaticProps or getServerSideProps or nextjs API routes as this comment said. https://www.prisma.io/nextjs#nextjs-tabs
I realized that the issue occurred because I forgot to use
'use server', as I was running a server action with Prisma.@olserra You’re replying to a public issue thread. Details about access restrictions aren’t relevant to this issue; be careful not to post any sensitive info here.
I had this same issue inside
getInitialPropswhen using import, as fix i am usingrequireinsidegetInitialProps:the
lib/prismafile is from the docs as @josiext mentionedsame issue