跳至主要内容

在 Monorepo 设置中使用 Prisma Client 在 Next.js 项目中

提示

如果您想学习如何使用 Next.js 和 Prisma ORM 构建应用程序,请查看这个全面的视频教程

问题

如果您在一个Next.js 应用程序内使用 Prisma Client,您可能会遇到类似于以下内容的错误

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

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

假设您有一个具有以下结构的 Monorepo

.
├── 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

上面的文件树显示了一个包含在 packages 文件夹中的 Monorepo。在其中,有两个包

  • db:在名为 client 的自定义输出位置包含生成的 Prisma Client。此包根目录中的 index.ts 导出实例化的 Prisma Client。
  • service:包含一个 Next.js 应用程序。test.js API 路由使用 db 包提供的 Prisma Client 实例。

上面提到的错误是由于 Next.js 捆绑过程中的捆绑问题造成的。查询引擎文件预计将在生成的 Prisma Client 旁边找到。但是,捆绑过程不会将这些文件复制到捆绑包的输出位置。

信息

有关捆绑过程中确切错误的更详细解释,请参阅我们为此问题在 Next.js GitHub 存储库中打开的。

解决方案

要解决此问题,您可以使用我们创建的自定义 Webpack 插件,该插件会将 Prisma Client 需要的文件正确复制到其正确的位置。

要使用此插件,请先安装该包

npm install -D @prisma/nextjs-monorepo-workaround-plugin

然后,您可以将插件导入到您的 next.config.js 文件中并在 config.plugins 中使用它。例如

const { PrismaPlugin } = require('@prisma/nextjs-monorepo-workaround-plugin')

module.exports = {
webpack: (config, { isServer }) => {
if (isServer) {
config.plugins = [...config.plugins, new PrismaPlugin()]
}

return config
},
}

或者,如果您使用的是 next.config.mjs,您可以按如下方式更改该文件

next.config.mjs
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