Skip to content

How to fix the error `PrismaClient is unable to be run in the browser` in Next.js

I ran into this error while working on a Next.js website:

PrismaClient is unable to be run in the browser

I had this page and it all worked fine until I commented one line in my code, in particular in my getStaticProps() method.

In that line I called a method from my Prisma instance, which I imported at the top of the page file.

Basically Next.js will see which code we use in getStaticProps() and will use it for the backend. It will not ship it to the frontend.

When I commented out the line in getStaticProps() where I used Prisma, Next.js included Prisma in my frontend code, and I got that error PrismaClient is unable to be run in the browser

The solution was to also comment (or remove) the Prisma import.

Also remember that getStaticProps() is only called on page routes, not in other components, so if this error comes from a component, you have to move the logic up to the page route component.

→ Download my free Next.js Handbook!

THE VALLEY OF CODE

THE WEB DEVELOPER's MANUAL

You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about next: