Using Prisma Client in a Next.js project in a monorepo setup
If you want to learn how to build an app with Next.js and Prisma ORM, check out this comprehensive video tutorial.
Problem
If you use Prisma Client in a Next.js application within a monorepo, you may run into an error that looks similar to:
Prisma Client could not locate the Query Engine for runtime "debian-openssl-3.0.x".
We detected that you are using Next.js, learn how to fix this: https://pris.ly/d/engine-not-found-nextjs.
This is likely caused by tooling that has not copied "libquery_engine-debian-openssl-3.0.x.so.node" to the deployment folder.
Ensure that you ran \`prisma generate\` and that "libquery_engine-debian-openssl-3.0.x.so.node" has been copied to "generated/client".
We would appreciate if you could take the time to share some information with us.
Please help us by answering a few questions: https://pris.ly/engine-not-found-tooling-investigation
or:
Prisma Client could not locate the Query Engine for runtime "debian-openssl-3.0.x".
We detected that you are using Next.js, learn how to fix this: https://pris.ly/d/engine-not-found-nextjs.
This is likely caused by a bundler that has not copied "libquery_engine-debian-openssl-3.0.x.so.node" next to the resulting bundle.
Ensure that "libquery_engine-debian-openssl-3.0.x.so.node" has been copied next to the bundle or in "generated/client".
We would appreciate if you could take the time to share some information with us.
Please help us by answering a few questions: https://pris.ly/engine-not-found-bundler-investigation
Assume you have a monorepo with the following structure:
.
├── packages
│ ├── db
│ │ ├── index.ts
│ │ ├── node_modules
│ │ ├── package.json
│ │ └── prisma
│ │ ├── client // <-- Custom output location for the generated Prisma Client
│ │ │ ├── index.js
│ │ │ ├── libquery_engine-debian-openssl-1.1.x.so.node // engine to be copied
│ │ │ └── schema.prisma // schema to be copied
│ │ └── schema.prisma
│ └── service/
│ ├── pages/
│ │ └── api/
│ │ └── test.js
│ ├── next.config.js
│ └── package.json
├── pnpm-workspace.yaml
├── package.json
└── vercel.json
The file tree above shows a monorepo contained in a packages
folder. Inside, there are two packages:
db
: Contains the generated Prisma Client in a custom output location namedclient
.index.ts
at the root of this package exports the instantiated Prisma Client.service
: Contains a Next.js application. Thetest.js
API route uses the Prisma Client instance provided by thedb
package.
The errors mentioned above occur as a result of a bundling problem during Next.js's bundling process. The Query Engine file(s) is expected to be found next to the generated Prisma Client. The bundling process, however, does not copy over those files to the output location of the bundle.
For a more detailed explanation of exactly what is going wrong during the bundling process, please refer to this issue we opened in the Next.js GitHub repository.
Solution
To work around this issue, you can use a custom Webpack plugin we created that correctly copies the files Prisma Client needs to their correct location.
To use this plugin, first install the package:
npm install -D @prisma/nextjs-monorepo-workaround-plugin
You can then import the plugin into your next.config.js
file and use it in config.plugins
. For example:
const { PrismaPlugin } = require('@prisma/nextjs-monorepo-workaround-plugin')
module.exports = {
webpack: (config, { isServer }) => {
if (isServer) {
config.plugins = [...config.plugins, new PrismaPlugin()]
}
return config
},
}
Alternatively, if you are using next.config.mjs
, you may alter the file as such:
import { PrismaPlugin } from '@prisma/nextjs-monorepo-workaround-plugin'
const nextConfig = {
webpack: (config, { isServer }) => {
if (isServer) {
config.plugins = [...config.plugins, new PrismaPlugin()]
}
return config
}
}
export default nextConfig