1

I'm trying to use Prisma in my NestJS project. Prisma initializes correctly, but whenever I run any Prisma CLI command (e.g., npx prisma, npx prisma migrate dev, etc.), I get this error:

Failed to load config file "D:\coding\backend-development\nestjs-blog-api" as a TypeScript/JavaScript module.
Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL

I already created a .env file in the project root and set the DATABASE_URL variable correctly. NestJS can read the environment variable without any issues — only Prisma CLI is failing.

My file structure:

project-root/
  prisma/
    schema.prisma
  .env
  package.json
  ...

.env contains:

DATABASE_URL="postgresql://user:pass@localhost:5432/mydb?schema=public"

I also tried:

  • Restarting the terminal

  • Running Prisma commands with --schema

  • Checking for casing issues

  • Deleting and recreating .env

But the error still appears.

What am I missing? How can I fix Prisma not detecting the .env file?

2
  • 1
    how are you using .env file, include reproducible code Commented Nov 14 at 19:12
  • you should include the version you are using Commented Nov 15 at 10:05

1 Answer 1

1

I had the exact same issue when using Prisma v6+ , which introduced the new prisma.config.ts setup. prisma.config.ts was not able to read my .env file, so Prisma kept throwing:

Missing required environment variable: DATABASE_URL

The fix was to manually load the environment variables inside prisma.config.ts using dotenv.

Here is the working configuration:

import { defineConfig } from "prisma/config";
import { config } from "dotenv";

// Load .env before defining the config
config();

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  engine: "classic",
  datasource: {
    url: process.env.DATABASE_URL || "",
  },
});

After adding config() at the top, Prisma CLI correctly picked up the DATABASE_URL environment variable and everything worked.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.