P
Prisma•2w ago
jcm10

Prisma Optimize Initialization Missing Database URL

After waiting 2 hours for migrations, I decided to give Optimize a go. I have my database url in my .env.development file, which works great when initializing my prisma clients in other files, the DB services. However, it can't find it on my index file.
// prettier-ignore
import dotenv from 'dotenv';
dotenv.config();

import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";

if (process.env.OPTIMIZE_API_KEY && process.env.DATABASE_URL) {
console.log(process.env.DATABASE_URL);
new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY})
)
}
// prettier-ignore
import dotenv from 'dotenv';
dotenv.config();

import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";

if (process.env.OPTIMIZE_API_KEY && process.env.DATABASE_URL) {
console.log(process.env.DATABASE_URL);
new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY})
)
}
Here is the error that gets printed:
PrismaClientInitializationError: error: Environment variable not found: DATABASE_URL.
PrismaClientInitializationError: error: Environment variable not found: DATABASE_URL.
However, if I remove the prisma code from this file and log the process.env.DATABASE_URL, it prints out no problem. The script I am running when the error occurs is this:
./node_modules/.bin/dotenv -e .env.development -- ts-node src/index.ts
./node_modules/.bin/dotenv -e .env.development -- ts-node src/index.ts
5 Replies
Prisma AI Help
Prisma AI Help•2w ago
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.
Nurul
Nurul•2w ago
Hey 👋 If I understand correctly, you get this error only when you are using optimize extension, right? If you remove withOptimize extension, everything works as expected? Also, which prisma and optimize version are you using?
christianalares
christianalares•6d ago
I just had the same issue and I thought looking in the discord might help and here we are! I also get the error Environment variable not found: DATABASE_URL but only when using the withOptimize-extension on the PrismaClient Using:
"@prisma/client": "^6.2.1",
"@prisma/extension-optimize": "^1.1.4",
"prisma": "^6.2.1"
"@prisma/client": "^6.2.1",
"@prisma/extension-optimize": "^1.1.4",
"prisma": "^6.2.1"
bighitbiker3
bighitbiker3•4d ago
@christianalares or @Nurul Any update on this? I am experiencing the same thing
jcm10
jcm10OP•4d ago
Hello! Here is how I fixed it.
new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
}).$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY })
);
new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
}).$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY })
);
Now I just need to figure out why migrations are taking 2 hours

Did you find this page helpful?