跳至主要内容

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

提示

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

问题

如果您在 Monorepo 中的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