How to fix the `Already 10 Prisma Clients are actively running` error

I was using Prisma in my Next.js app and I was doing it wrong.

I was initializing a new PrismaClient object in every page:

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

After some point, during app usage, I received the error Already 10 Prisma Clients are actively running and also a Address already in use.

To fix this, I exported this Prisma initialization to a separate file, lib/prisma.js:

import { PrismaClient } from '@prisma/client'

let prisma

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient()
  }
  prisma = global.prisma
}

export default prisma

The production check is done because in development, npm run dev clears the Node.js cache at runtime, and this causes a new PrismaClient initialization each time due to hot reloading, so we’d not solve the problem.

I took this code from https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices

Finally I imported the exported prisma object in my pages:

import prisma from 'lib/prisma'


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I was using Prisma in my Next.js app and I was doing it wrong.

I was initializing a new PrismaClient object in every page:

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

After some point, during app usage, I received the error Already 10 Prisma Clients are actively running and also a Address already in use.

To fix this, I exported this Prisma initialization to a separate file, lib/prisma.js:

import { PrismaClient } from '@prisma/client'

let prisma

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient()
  }
  prisma = global.prisma
}

export default prisma

The production check is done because in development, npm run dev clears the Node.js cache at runtime, and this causes a new PrismaClient initialization each time due to hot reloading, so we’d not solve the problem.

I took this code from https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices

Finally I imported the exported prisma object in my pages:

import prisma from 'lib/prisma'


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-05-15T05:00:00+00:00) How to fix the `Already 10 Prisma Clients are actively running` error. Retrieved from https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/

MLA
" » How to fix the `Already 10 Prisma Clients are actively running` error." flaviocopes.com | Sciencx - Saturday May 15, 2021, https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/
HARVARD
flaviocopes.com | Sciencx Saturday May 15, 2021 » How to fix the `Already 10 Prisma Clients are actively running` error., viewed ,<https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/>
VANCOUVER
flaviocopes.com | Sciencx - » How to fix the `Already 10 Prisma Clients are actively running` error. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/
CHICAGO
" » How to fix the `Already 10 Prisma Clients are actively running` error." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/
IEEE
" » How to fix the `Already 10 Prisma Clients are actively running` error." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/. [Accessed: ]
rf:citation
» How to fix the `Already 10 Prisma Clients are actively running` error | flaviocopes.com | Sciencx | https://www.scien.cx/2021/05/15/how-to-fix-the-already-10-prisma-clients-are-actively-running-error/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.