PrismaClient is unable to be run in the browser.

See original GitHub issue

Bug description

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

Selection_013

How to reproduce

  1. Create new Node Project with npm init -y
  2. install dependencies npm i next react react-dom
  3. install prisma with npm i -D primsa and then npm i @prisma/client
  4. init prisma with npx primsa init
  5. 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?
}

  1. push schema to postgresql with npx prisma db push --preview-feature
  2. 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>
}





  1. run server
  2. 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
ninestcommented, May 13, 2021

I was having a similar issue. I think a solution is to only initialize the client once and on the server only.

import { Prisma, PrismaClient } from "@prisma/client";

declare global {
  namespace NodeJS {
    interface Global {
      prisma: PrismaClient;
    }
  }
}

let prisma: PrismaClient;

if (typeof window === "undefined") {
  if (process.env.NODE_ENV === "production") {
    prisma = new PrismaClient();
  } else {
    if (!global.prisma) {
      global.prisma = new PrismaClient();
    }

    prisma = global.prisma;
  }
}

export default prisma;
12reactions
pantharshit00commented, Mar 26, 2021

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the error `PrismaClient is unable to be run in the ...
In that line I called a method from my Prisma instance, which I imported at the top of the page file. Basically Next.js...
Read more >
Best practice for instantiating PrismaClient with Next.js
In development, the command next dev clears Node.js cache on run. This in turn initializes a new PrismaClient instance each time due to...
Read more >
Server code in *.server.ts getting bundled in Remix JS Client
I am getting an error that says: PrismaClient is unable to be run in the browser. even though PrismaClient is being run inside...
Read more >
@prisma/client - npm
Alternatively you can explore the ready-to-run examples (REST, GraphQL, gRPC, plain JavaScript and TypeScript demos, ...) or watch the demo ...
Read more >
How to use Prisma in a Next.js app - Daily.dev
We will now use Prisma client library to query the database. ... Error: PrismaClient is unable to be run in the browser.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found