"Missing required environment variable: DATABASE_URL" after Migrating to prisma.config.ts

I'm trying to migrate to the new prisma.config.ts as outlines here. I'm running into this error.
Failed to load config file "D:\Projects\cafeBot\api" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL
Failed to load config file "D:\Projects\cafeBot\api" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL
This error occurs when I run npx prisma generate. It was perviously working before moving to prisma.config.ts. prisma.config.ts
import { defineConfig, env } from "prisma/config";
import * as path from "node:path";
export default defineConfig({
// The Rust-compiled schema engine
engine: "classic",
schema: path.join("prisma", "schema.prisma"),
migrations: {
path: path.join("prisma", "migrations"),
seed: "ts-node -r tsconfig-paths/register prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});
import { defineConfig, env } from "prisma/config";
import * as path from "node:path";
export default defineConfig({
// The Rust-compiled schema engine
engine: "classic",
schema: path.join("prisma", "schema.prisma"),
migrations: {
path: path.join("prisma", "migrations"),
seed: "ts-node -r tsconfig-paths/register prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});
prisma client
import { PrismaClient } from "@prisma/client"
import {PrismaPg} from "@prisma/adapter-pg";

const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }

const env = process.env.NODE_ENV;

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
export const prisma = globalForPrisma.prisma || new PrismaClient({
adapter: adapter,
log:
env === "development"
? ["warn", "error", "query", "info"]
: env === "test"
? [] // no logs in test
: ["warn", "error"], // production / other
});

if (env !== "production") globalForPrisma.prisma = prisma;
import { PrismaClient } from "@prisma/client"
import {PrismaPg} from "@prisma/adapter-pg";

const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }

const env = process.env.NODE_ENV;

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
export const prisma = globalForPrisma.prisma || new PrismaClient({
adapter: adapter,
log:
env === "development"
? ["warn", "error", "query", "info"]
: env === "test"
? [] // no logs in test
: ["warn", "error"], // production / other
});

if (env !== "production") globalForPrisma.prisma = prisma;
Prisma
Prisma ORM 6.18.0, Postgres Database Metrics and Config Modernization
Catch up on the lates 6.18.0 release for Prisma ORM and Prisma Postgres.
1 Reply
Prisma AI Help
Prisma AI Help2mo ago
You chose to compile with the bot. One moment while I piece together your answer… The error "Missing required environment variable: DATABASE_URL" after migrating to prisma.config.ts is expected behavior. When you use a Prisma config file (prisma.config.ts), Prisma no longer automatically loads environment variables from .env files. You must explicitly load them yourself. Solution:
Add dotenv to your project and import it at the top of your prisma.config.ts:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
import * as path from "node:path";

export default defineConfig({
engine: "classic",
schema: path.join("prisma", "schema.prisma"),
migrations: {
path: path.join("prisma", "migrations"),
seed: "ts-node -r tsconfig-paths/register prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
import * as path from "node:path";

export default defineConfig({
engine: "classic",
schema: path.join("prisma", "schema.prisma"),
migrations: {
path: path.join("prisma", "migrations"),
seed: "ts-node -r tsconfig-paths/register prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});
Why?
As documented, with prisma.config.ts, environment variables from .env files are not loaded automatically. You must use dotenv or another method to load them yourself. This is a documented change and is also discussed in community issues and the official docs [source][source]. Summary of steps: 1. Install dotenv:
npm install dotenv 2. Add import "dotenv/config"; to the top of your prisma.config.ts. This should resolve the "Missing required environment variable: DATABASE_URL" error when running Prisma CLI commands after migrating to prisma.config.ts. ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?