Fix 'Already 10 Prisma Clients are actively running'

By

Fix the Next.js Already 10 Prisma Clients are actively running error by exporting one shared PrismaClient instance and reusing it across hot reloads.

~~~

The Already 10 Prisma Clients are actively running error means your Next.js app keeps creating new PrismaClient instances instead of reusing one. The fix is a single shared instance, exported from one file.

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.

Why does this happen?

Each PrismaClient instance opens its own pool of connections to the database. One instance is all an app needs.

In development, hot reloading makes things worse. Every time you save a file, npm run dev clears the Node.js module cache and re-runs your code. Each reload created a brand new PrismaClient, while the old ones stayed alive holding their connections. After enough saves, Prisma refused to start another one.

The fix: one shared instance

To fix this, I exported the Prisma initialization to a separate file, lib/prisma.js (or lib/prisma.ts). Here’s the version that matches the current Prisma docs:

import { PrismaClient } from '../prisma/generated/client'

const globalForPrisma = globalThis

export const prisma = globalForPrisma.prisma ?? new PrismaClient()

if (process.env.NODE_ENV !== 'production') {
  globalForPrisma.prisma = prisma
}

export default prisma

Notice the import path. Since Prisma 7 the output field in the generator block is required, so the client lives wherever you point it and you import it from there. Older projects import from @prisma/client instead, like my pages did. Use whichever path prisma generate creates for you.

Prisma 7 also wants a driver adapter, so in a real project the call is new PrismaClient({ adapter }). The sharing logic is the same either way.

In production the module is only loaded once, so creating the client at module level is enough.

In development we store the instance on globalThis. That object survives hot reloads, while module-level variables don’t. On the next reload the code finds the existing client and reuses it, instead of creating client number eleven.

I took this pattern from https://www.prisma.io/docs/orm/more/help-and-troubleshooting/nextjs-help

Finally I imported the exported prisma object in my pages:

import prisma from 'lib/prisma'

One thing to watch for

The fix only works if every file imports from lib/prisma.js. If a single API route still calls new PrismaClient() on its own, that route keeps leaking connections, and the error comes back.

Search your project for new PrismaClient and make sure it appears exactly once, inside lib/prisma.js.

Tagged: Next.js · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about next: