question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PrismaClient is unable to be run in the browser.

See original GitHub issue

Bug description

image image

Expected behavior

A user authentication page should be displayed.

Prisma information

image

schema.prisma

image image

Environment & setup

  • OS: windows 10 v1909 image

  • Database: Heroku postgresql image

  • Node.js version: v 15.4.0 image

  • Prisma version: 2.17.0 image


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
ninestcommented, May 13, 2021

I’m not sure if anyone is still facing this issue, but I solved it by initializing prisma 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;

Use if (typeof window === "undefined") { to run code on the server but not on the browser.

2reactions
younes-alouanicommented, Jun 19, 2022

There should be a PR for this !

Read more comments on GitHub >

github_iconTop Results From Across the Web

PrismaClient is unable to be run in the browser. #6219 - GitHub
Bug description I'm getting an Error: PrismaClient is unable to be run in the browser. How to reproduce Create new Node Project with...
Read more >
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 >

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