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 all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.