Skip to content

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'
→ Get my Next.js (pages router) Handbook

I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list