Prisma queries crash Next.js static build — export const dynamic required
Problem
Server components and API routes that use Prisma cause next build to fail because Next.js tries to statically render them at build time. DATABASE_URL is not available during the build phase, so Prisma cannot connect. Any file importing prisma must be marked as dynamic to opt out of static generation.
Error Output
Error: PrismaClientInitializationError: Can't reach database server at `localhost:5432` Please make sure your database server is running at `localhost:5432`.
Unverified for your environment
Select your OS to check compatibility.
3 Fixes
Mark Prisma-using routes as dynamic to prevent build-time DB access
Build-time static evaluation of modules that touch Prisma. Any route, server component, or imported helper that reaches Prisma during build must opt out of static generation.
Trust Score
1 verification
- 1
Add dynamic export to affected routes
Add export const dynamic to every route or page that imports Prisma directly or via a helper.
codeexport const dynamic = "force-dynamic"; - 2
Audit import chains
Check for indirect imports — a route that imports a helper that imports Prisma will also need the dynamic export.
- 3
Verify build passes
Run next build and confirm no Prisma-related crash during static generation.
Validation
Marking Prisma-using routes as dynamic prevents build-time DB access and resolves static build crashes when DATABASE_URL is unavailable during build.
Verification Summary
Sign in to verify this fix
Alternative Fixes
Add export const dynamic = "force-dynamic" to all Prisma-using pages and routes
Next.js 14 App Router tries to statically generate every page at build time. Prisma requires DATABASE_URL at runtime. Marking the route as dynamic tells Next.js to render it at request time only.
Trust Score
3 verifications
- 1
Add the export to every file that imports prisma
Add this line at the very top of each page.tsx or route.ts that queries the database:
typescriptexport const dynamic = 'force-dynamic' // then your normal imports and code import { prisma } from '@/lib/prisma' - 2
Find all files that need it
Search for all files importing prisma:
bashgrep -rl "from '@/lib/prisma'" src/app/ - 3
Run next build to verify
Build should complete. Pages with force-dynamic are marked with ƒ (Dynamic) in the build output.
Validation
next build succeeds. Dynamic routes are marked ƒ in build output, not ○ (Static).
Verification Summary
Sign in to verify this fix
1 low-confidence fix
Mark Prisma-using routes and components as dynamic to prevent build-time DB access
Next.js static build attempts to pre-render routes at build time. Any route, Server Component, or imported helper that imports Prisma will try to connect to the database during build — which fails because DATABASE_URL is unavailable or the DB is not reachable in CI. The fix is to opt these routes out of static generation.
Awaiting Verification
Be the first to verify this fix
- 1
Add export const dynamic to affected routes
Add this export to every page.tsx, route.ts, or layout.tsx that directly or indirectly imports Prisma.
typescript// app/dashboard/page.tsx export const dynamic = 'force-dynamic' // This prevents Next.js from pre-rendering this route at build time - 2
Audit your import graph for build-time Prisma access
Check for shared utility files or service modules that instantiate prisma at module load time. If imported in a statically-rendered route, they will cause the same crash. Move prisma instantiation inside functions, not at module scope.
- 3
Guard prisma instantiation in lib/prisma.ts
Ensure the prisma singleton is only created at request time, not module evaluation time. Use a lazy initialization pattern.
typescript// lib/prisma.ts import { PrismaClient } from '@prisma/client' const globalForPrisma = globalThis as unknown as { prisma: PrismaClient } export const prisma = globalForPrisma.prisma ?? new PrismaClient() if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
Validation
next build completes without database connection errors. Static routes pre-render successfully. Dynamic routes with Prisma serve correctly at runtime.
Sign in to verify this fix
Environment
- Product
- Next.js + Prisma
- Version
- 14.x
- Environment
- development
Submitted by
Alex Chen
2450 rep