部署至Vercel遭遇PrismaClientInitializationError问题求助
解决Vercel部署时PrismaClientInitializationError问题
核心解决方案:修改构建脚本强制生成Prisma Client
问题根源是Vercel的依赖缓存机制跳过了Prisma Client的自动生成步骤,导致使用过期客户端版本。只需修改package.json中的build命令,在构建前执行prisma generate:
"scripts": { "dev": "next dev", "build": "prisma generate && next build", "start": "next start", "lint": "next lint" }
补充说明
- 你写的全局Prisma Client实例代码是正确的,问题不在实例创建逻辑,而是构建阶段缺少生成步骤。
prisma generate会根据当前schema.prisma文件重新生成适配的客户端代码,覆盖Vercel缓存中的旧版本。- 可先在本地运行
npm run build测试,确认构建流程能正常生成客户端并完成项目构建,排除本地配置问题。
问题背景
部署到Vercel时触发PrismaClientInitializationError,错误提示Vercel缓存导致Prisma Client未自动生成。已创建全局PrismaClient实例:
import { PrismaClient } from "@prisma/client"; declare global { var prisma: PrismaClient | undefined; } const client = globalThis.prisma || new PrismaClient(); if (process.env.NODE_ENV !== "production") globalThis.prisma = client; export default client;
完整错误日志:
PrismaClientInitializationError: Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the `prisma generate` command during the build process. at eu (/vercel/path0/node_modules/@prisma/client/runtime/library.js:164:69) at new t (/vercel/path0/node_modules/@prisma/client/runtime/library.js:177:3203) at 40754 (/vercel/path0/.next/server/chunks/578.js:138:37) at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:43) at 76578 (/vercel/path0/.next/server/chunks/578.js:23:16) at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:43) at 32631 (/vercel/path0/.next/server/app/(site)/home/page.js:4370:22) at Function.__webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:43) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async collectGenerateParams (/vercel/path0/node_modules/next/dist/build/utils.js:822:17) { clientVersion: '4.15.0', errorCode: undefined } > Build error occurred Error: Failed to collect page data for /home at /vercel/path0/node_modules/next/dist/build/utils.js:1155:15 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { type: 'Error' } Error: Command "npm run build" exited with 1 BUILD_UTILS_SPAWN_1: Command "npm run build" exited with 1
内容的提问来源于stack exchange,提问作者Mathew
相关产品推荐
相关产品推荐

