PrismaClientInitializationError]: error: Environment variable not found: DATABASE_URL.
I really need help, i spent like 2 hours now on this and i just cant fix it.
1. I am on the latest's version of the client
2. I have checked, i have a .env in root and it has DATABASE_URL in it
3. I tried using the new prisma.config.ts (and without):
Still same error...
This is my main schema:
This is how i initialise it:
I use these pool things from the docs. It is becouse i am deploying to Vercel. And vercel was giving some weird issues that i was trying to resolve. But now i have these issues.
The prior issues that lead to me upgrading the @prisma/client version and having these problems:
It says no exported member. While it does, ofcouse. Locally it DOES build and export the types. So idk why it didnt on vercel. So i just updated the client in hopes of fixing it. But now got more problems
2 Replies
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
It’s almost always one of these causes:
1) .env not loaded at runtime
- Prisma needs DATABASE_URL available when PrismaClient is instantiated. Ensure your runtime loads .env (or that your platform injects envs). Try adding dotenv before any Prisma import in the entrypoint, not only in prisma.config.ts (that file affects generate/CLI, not your app runtime) (dotenv loading regressions and fixes are documented; see 5.16.0 → 5.16.1) (dotenv loading issue) (dotenv loading issue fixed in 5.16.1).
2) Using a different env var name on Vercel
- Vercel Postgres uses POSTGRES_PRISMA_URL by default, not DATABASE_URL. Either:
- change your schema to use env("POSTGRES_PRISMA_URL"), or
- set DATABASE_URL in Vercel to the same value. Also consider directUrl if needed (pool/non-pool) (Vercel Postgres env names).
3) Prisma evaluates env() even if you pass an adapter/pool
- Prisma still resolves datasource url from schema. If DATABASE_URL is missing, it fails even with adapter configured. Set the env or hardcode temporarily to confirm (historic behavior) (env() evaluated even when overriding).
4) Optimize extension included in deploy
- Do not deploy with @prisma/extension-optimize enabled. It caused “Environment variable not found: DATABASE_URL” for some users; removing it fixed deployments (Optimize caused env error).
5) Turborepo / build-time envs
- Ensure DATABASE_URL is available at build/runtime in Vercel. In monorepos, propagate envs (e.g., turbo.json globalEnv). But Prisma Client resolves env at runtime, so verify the route sees process.env.DATABASE_URL and that Prisma still errors—if so, it’s likely (2) or (4) (monorepo/Vercel thread).
Action checklist
- Locally: run wit
h dotenv early (e.g., require('dotenv/config')) and
DEBUG="prisma:tryLoadEnv"
to verify loading paths (see logs in the dotenv issue) (dotenv loading issue).
- On Vercel:
- If using Vercel Postgres: switch schema to POSTGRES_PRISMA_URL (and optional directUrl) as per docs, or set DATABASE_URL accordingly (Vercel Postgres env names).
- Remove Optimize from production: .$extends(withOptimize(...)) (Optimize caused env error).
- Add "postinstall": "prisma generate --no-engine" to ensure client exists at deploy (Vercel + Prisma Postgres).
About the earlier “no exported member” error on Vercel
- Ensure prisma generate runs on deploy (postinstall), and that the generated client version matches @prisma/client version to have types available in CI/CD (postinstall generate on Vercel).
If after removing Optimize and fixing env name you still see the error, please share prisma:tryLoadEnv logs; there are known cases where env resolution behaved inconsistently across versions, fixed by upgrading and regenerating (5.16.1 fix).
✅ Great, thanks for letting us know that this solved your question!My.... god....... It was @prisma/extension-optimize that caused the issue...........
Okay, vercel error still ongoing tho