Prisma CLI fails with PrismaConfigEnvError about DATABASE_URL
PrismaConfigEnvError: Missing required environment variable: DATABASE_URLShort answer
Prisma 7 does not read .env files by itself. prisma.config.ts is loaded by every CLI command, and env('DATABASE_URL') throws when the variable is not in process.env at that moment. Load the .env file at the top of the config, or pass the variable on the command line, or stop using env() for commands that need no database.
Fix it fast
1Find out which cause you have
Check whether the variable is visible to a plain Node.js process started from the project folder. Prisma CLI sees the same environment, not what Next.js loads.
node -e "console.log(process.env.DATABASE_URL ? 'set' : 'missing')"2Match what you see, then jump to its fix
- 1
DATABASE_URL is in .env, yet npx prisma migrate dev or prisma generate fails at onceThe .env file is never loaded by prisma.config.tsFix - 2
The error appears in CI during prisma generate, where no database is configured on purposeA command that needs no database still runs env()Fix - 3
The error appears on migrate deploy in a pipeline or on a serverA command that connects is run without the variableFix
- 1
What does the error look like?
PrismaConfigEnvError: Missing required environment variable: DATABASE_URLPrismaConfigEnvError: Cannot resolve environment variable: DATABASE_URL
Cause 1 of 3
The .env file is never loaded by prisma.config.ts
1Check
Open prisma.config.ts and look for an import that loads the file. Prisma 7 no longer loads .env on its own, and Next.js only loads it inside its own runtime, not for the Prisma CLI.
head -n 5 prisma.config.ts2Fix
Add the dotenv import as the first line of prisma.config.ts, so the variables exist before env() runs. In a Next.js project that keeps secrets in .env.local, use @next/env instead, which follows the same file order as Next.js.
// prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: { url: env("DATABASE_URL") },
});
// Next.js alternative, with .env.local support:
// import { loadEnvConfig } from "@next/env";
// loadEnvConfig(process.cwd());3Verify
Run a command that reads the config.
npx prisma validateConfirmed by prisma.io
Cause 2 of 3
A command that needs no database still runs env()
1Check
Every CLI command loads the config file, so env() throws even for prisma generate, which never connects. Check whether your CI job really sets the variable.
grep -n "env(" prisma.config.ts2Fix
Read the variable with process.env instead of env(), with an empty-string fallback, so generate works without a database. Keep the real value for migrate and deploy steps.
// prisma.config.ts
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: { url: process.env.DATABASE_URL ?? "" },
});3Verify
Run generate with the variable unset.
env -u DATABASE_URL npx prisma generateConfirmed by prisma.io
Cause 3 of 3
A command that connects is run without the variable
1Check
A deploy shell does not read your local .env. Confirm the variable is exported in that shell.
printenv DATABASE_URL | cut -c1-122Fix
Set the variable on the command itself, or export it in the pipeline settings.
DATABASE_URL="postgresql://user:pass@host:5432/db" npx prisma migrate deploy3Verify
The command now connects instead of failing at config load.
DATABASE_URL="postgresql://user:pass@host:5432/db" npx prisma migrate statusConfirmed by prisma.io
How do I stop it coming back?
- Keep the dotenv or @next/env line as the first statement of prisma.config.ts.
- Set DATABASE_URL in the CI job for steps that connect, and leave it out of steps that only generate.
- Do not copy a real connection string into docs or chat when asking for help; use a placeholder.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fixBack end
Show a custom sign-in error in NextAuth without leaving the page
http://localhost:3000/api/auth/error?error=...
By default a failed NextAuth credentials sign-in navigates to the error page with the message in the URL.
3 causes, each with a fixBack end
Request body is rejected as too large in Next.js (1 MB default)
413 Payload Too Large
Next.js limits request bodies to 1 MB by default, in both Pages Router API routes and Server Actions.
3 causes, each with a fixBack end
Next.js server code cannot connect to its own API on 127.0.0.1
Error: connect ECONNREFUSED 127.0.0.1:3003
ECONNREFUSED means nothing was listening at that address and port when the server-side fetch ran.
2 causes, each with a fixBack end
next build stops with: Failed to collect page data for a route
Error: Failed to collect page data for /dashboard
Failed to collect page data for a route is the last line of a failed build, not the cause.
3 causes, each with a fixBack end
TypeError: Failed to parse URL when a Next.js server fetches its own API
TypeError: Failed to parse URL from undefined/api/test
Server code has no page address, so fetch('/api/test') cannot be turned into a full URL and throws Failed to parse URL.
3 causes, each with a fix