FG
🌐 Web & Full-StackVerceldevelopment

Prisma queries crash Next.js static build — export const dynamic required

Freshabout 16 hours ago
Mar 14, 20260 views
Confidence Score65%
65%

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

Canonical Fix
Moderate Confidence Fix
70% confidence100% success rate1 verificationLast verified Mar 17, 2026

Mark Prisma-using routes as dynamic to prevent build-time DB access

Low Risk

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.

70

Trust Score

1 verification

100% success
  1. 1

    Add dynamic export to affected routes

    Add export const dynamic to every route or page that imports Prisma directly or via a helper.

    code
    export const dynamic = "force-dynamic";
  2. 2

    Audit import chains

    Check for indirect imports — a route that imports a helper that imports Prisma will also need the dynamic export.

  3. 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

Worked: 1
Last verified Mar 17, 2026

Sign in to verify this fix

Alternative Fixes

Moderate Confidence Fix
64% confidence50% success rate3 verificationsLast verified Mar 14, 2026

Add export const dynamic = "force-dynamic" to all Prisma-using pages and routes

Low Risk

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.

64

Trust Score

3 verifications

50% success
  1. 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:

    typescript
    export const dynamic = 'force-dynamic'
    
    // then your normal imports and code
    import { prisma } from '@/lib/prisma'
  2. 2

    Find all files that need it

    Search for all files importing prisma:

    bash
    grep -rl "from '@/lib/prisma'" src/app/
  3. 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

Worked: 3
Failed: 3
Last verified Mar 14, 2026

Sign in to verify this fix

1 low-confidence fix
Unverified Fix
New Fix – Awaiting Verification

Mark Prisma-using routes and components as dynamic to prevent build-time DB access

Low Risk

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

AC

Alex Chen

2450 rep

Tags

nextjsprismaforce-dynamicbuild-errordatabase-url