You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vercel部署的Next.js应用中Prisma无法读取DATABASE_URL的原因及正确配置方案咨询

Troubleshooting Prisma's DATABASE_URL Not Found in Vercel-deployed Next.js App Router

I’ve run into this exact issue before with Next.js App Router, Prisma, and Neon on Vercel—let’s break down the most likely culprits and fixes:

1. Ensure Vercel Environment Variables Are Available at Runtime

Vercel splits environment variables into two scopes: Build Time and Runtime. By default, some variables might only be enabled for Build Time, but Prisma needs DATABASE_URL at runtime (especially for Server Components, API Routes, or Server Actions that run on Vercel’s edge/Node.js functions).

  • Head to your Vercel project dashboard → Settings → Environment Variables
  • Find your DATABASE_URL entry, click the three dots → Edit
  • Make sure the Runtime checkboxes (for both Production and Preview, depending on which environment you’re testing) are checked. If only Build Time is enabled, your runtime code won’t see the variable.

2. Fix Your Build Command to Generate Prisma Client First

Prisma Client needs to be generated with awareness of your environment variables during the build process. Skipping prisma generate can leave the client misconfigured for runtime use.

Update your Vercel build command to:

prisma generate && prisma migrate deploy && next build
  • prisma generate creates a Prisma Client tailored to your schema and environment
  • prisma migrate deploy runs your database migrations (reserve this for production/preview environments—use prisma migrate dev locally)

3. Verify Your Prisma Schema Configuration

Double-check your schema.prisma to ensure you’re not overriding the connection string with conflicting parameters. Your datasource block should look exactly like this:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
  • Don’t add extra fields like host, user, password, or dbname here—these will override values from your DATABASE_URL string, leading to the "undefined" values in your error message.

4. Validate Your Neon Connection String Format

Make sure you’re using the Pooled Connection String from your Neon dashboard (it usually ends with ?pooler=true). Reserve DATABASE_URL_UNPOOLED only for migrations, as connection pools don’t play well with migration workflows.

Double-check the string includes:

  • Your Neon username (not undefined)
  • Your database name (not undefined)
  • The correct password (not null)
  • The Neon host (typically formatted like ep-cool-darkness-123456.us-east-2.aws.neon.tech)

5. Debug Runtime Environment Variables

To confirm if DATABASE_URL is actually available at runtime, add a quick debug check in a Server Component or API Route:

// app/add-your-store/page.tsx (Server Component)
export default async function AddStorePage() {
  // Debug: Log the env var (check Vercel Function Logs after deployment)
  console.log("DATABASE_URL at runtime:", process.env.DATABASE_URL);
  
  // Rest of your component code...
}
  • Deploy this change, then go to Vercel → Functions → Logs for the relevant function
  • If the log shows undefined, your env variable isn’t reaching runtime—go back to step 1 to fix Vercel’s env variable scoping
  • If it shows the full connection string, the issue is likely with Prisma’s configuration (step 3) or connection string format (step 4)

6. Avoid NEXT_PUBLIC_ Prefix for DATABASE_URL

Never use NEXT_PUBLIC_DATABASE_URL—this exposes your database credentials to the client-side, which is a critical security risk. Prisma runs exclusively server-side, so DATABASE_URL should stay as a non-public environment variable.


内容的提问来源于stack exchange,提问作者user32635670

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 06:38:35