I’m working on a Next.js + TypeScript project with a MySQL database using Prisma.
I set up my configuration in prisma.config.ts like this:
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
engine: "classic",
datasource: {
url: env("DATABASE_URL"),
},
});
I have this error:
PrismaConfigEnvError: Missing required environment variable: DATABASE_URL
I was missing this line:
import "dotenv/config";
I ran:
npm i dotenv
and added the above line.
My connection was established after I ran:
npx prisma db push
When using Prisma with SQLite, you might get this error:
PrismaConfigEnvError: Missing required environment variable: DATABASE_URL.
Even if you've set DATABASE_URL in your .env file.
This happens, because Prisma can't load environment variables from the .env file. This is how I fixed the issue:
First, I installed dotenv: npm i dotenv
In my prisma.config.ts file, I imported dotenv and call config() to load the environment variables like this:
import { defineConfig } from "prisma/config";
import { config } from "dotenv";
config();
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
engine: "classic",
datasource: {
url: process.env.DATABASE_URL,
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With