fix(01-01): load .env manually in prisma.config.ts — Prisma 7 env() unreliable on Windows

This commit is contained in:
admin committed 2026-06-06 09:29:39 +02:00
1 parent 1b5f1affe9
commit 091c2dc0e4
2 files changed
+27 -3

No files matched your search

+26
View File
@@ -1,5 +1,31 @@
import { defineConfig } from "prisma/config";
import { readFileSync, existsSync } from "fs";
import { resolve } from "path";
// Prisma 7's env() helper doesn't reliably load .env on all platforms.
// Load it manually so DATABASE_URL is available when this config is evaluated.
function loadEnvFile(filename: string) {
const envPath = resolve(process.cwd(), filename);
if (!existsSync(envPath)) return;
const lines = readFileSync(envPath, "utf-8").split(/\r?\n/);
for (const line of lines) {
const match = line.match(/^([^#\s][^=]*)=(.*)$/);
if (!match) continue;
const key = match[1].trim();
const value = match[2].trim().replace(/^["']|["']$/g, "");
if (!(key in process.env)) process.env[key] = value;
}
}
loadEnvFile(".env");
loadEnvFile(".env.local"); // Next.js convention — takes priority
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) throw new Error("DATABASE_URL is not set in .env or .env.local");
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: databaseUrl,
},
});
+1 -3
View File
@@ -3,9 +3,7 @@ generator client {
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
provider = "postgresql"
}
model Owner {