I am trying to deploy a Next.js frontend from my Turborepo monorepo to Vercel. The deployment fails during the build step when it tries to run prisma generate.
The error log clearly shows that the DATABASE_URL environment variable is missing, even though I have it defined in my local .env files and have also added the variable to my Vercel project settings.
The Error Log from Vercel
@repo/db:build:
@repo/db:build: > @repo/db@ build /vercel/path0/packages/db
@repo/db:build: > prisma generate && tsc
@repo/db:build:
@repo/db:db:generate: [[email protected]] injecting env (0) from .env.example -- tip: ⚙️ override existing env vars with { override: true }
@repo/db:db:generate: Failed to load config file "/vercel/path0/packages/db" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL
@repo/db:db:generate: ELIFECYCLE Command failed with exit code 1.
@repo/db:db:generate: ERROR: command finished with error: command (/vercel/path0/packages/db) /pnpm10/node_modules/.bin/pnpm run db:generate exited (1)
@repo/db:build: [[email protected]] injecting env (0) from .env.example -- tip: 🛠️ run anywhere with `dotenvx run -- yourcommand`
@repo/db:build: Loaded Prisma config from prisma.config.ts.
@repo/db:build:
@repo/db:build: ELIFECYCLE Command failed with exit code 130.
@repo/db#db:generate: command (/vercel/path0/packages/db) /pnpm10/node_modules/.bin/pnpm run db:generate exited (1)
packages/db/prisma.config.ts:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
import fs from "fs";
import dotenv from "dotenv";
if (fs.existsSync(".env")) {
dotenv.config({ path: ".env" });
} else {
dotenv.config({ path: ".env.example" });
}
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
engine: "classic",
datasource: {
url: env("DATABASE_URL"),
},
});
And here is my root turbo.json. I've tried adding the environment variable here as well.
turbo.json
{
"$schema": "https://turborepo.com/schema.json",
"ui": "tui",
"tasks": {
"build": {
"dependsOn": ["^build", "^db:generate"],
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": ["dist/**", ".next/**", "!.next/cache/**"],
"env": ["DATABASE_URL", "FETCH_BASE_URL", "JWT_SECRET"]
},
"lint": {
"dependsOn": ["^lint"]
},
"check-types": {
"dependsOn": ["^check-types"]
},
"dev": {
"dependsOn": ["^db:generate"],
"cache": false,
"persistent": true
},
"db:generate": {
"cache": false
},
"db:migrate": {
"cache": false,
"persistent": true
},
"db:deploy": {
"cache": false
}
}
}