30

I am getting this error message from prisma when I am running the GraphQL query.

Environment variable not found: DATABASE_URL.\n  -->  schema.prisma:6\n   | \n 5 |   provider = \"postgresql\"\n 6 |   url      = env(\"DATABASE_URL\")\n   | \n\nValidation Error Count: 1",

At first, I didn't have the .env file in any of my project folders, then I added it with the link to the database url, still not working. Here is the folder structure:

The folder structure: in the root, there's node_modules, prisma and src folders, .env, .gitignore and package.json files. Inside prisma, there's a migrations folder and a schema.prisma file.

This is what I have inside my .env file looks like -

DATABASE_URL="postgres://postgres:mypassword@db.pqtgawtgpfhpqxpgidrn.supabase.co:5432/postgres"
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Sumchans
  • 3,088
  • 6
  • 32
  • 59

7 Answers7

95

If anybody running into this issue, just run npx prisma generate. This will re-establish the link between schema.prisma and .env file.

azium
  • 20,056
  • 7
  • 57
  • 79
Sumchans
  • 3,088
  • 6
  • 32
  • 59
  • 2
    For me it turned out that I had `DATABASE_URL="my value` and had forgotten the closing double-quote! So the value wasn't "there" because `dotenv` wasn't able to parse it correctly because of a mistake on my part. – aproximation Jan 04 '22 at 21:54
  • 1
    If you are a next.js user, then see the answers below – John Miller Jun 21 '23 at 23:02
  • for me, it's not working on next because I'm running the command in another folder different than the root project. It works perfectly running `npx prisma generate` in the correct path. – equiman Jul 03 '23 at 07:18
30

In my case I wanted to run Prisma Studio with NextJS that stores all environment variables in .env.local, so I need to load the file first.

npm install -g dotenv-cli
dotenv -e .env.local -- npx prisma studio

Here is a link to the official Prisma docs on how to load .env files manualy.

Pecata
  • 683
  • 8
  • 13
  • 5
    Thank you! This was super helpful I added `"migrate": "dotenv -e .env.local npx prisma migrate dev"` to the scripts in my package.json file as well. – fotoflo Oct 12 '22 at 03:46
  • 2
    thanks @fotoflo, I guess adding scripts in package.json file makes it super easy. – Anmol kansal Nov 20 '22 at 07:52
28

I had this issue in my NextJs project. after changing the .env.local file to .env everything worked.

elyas.m
  • 1,260
  • 1
  • 13
  • 20
0

Others like me (new to Prisma, following the Remix.run jokes-app tutorial) might be relieved to learn it's not just you: there was a regression in Prisma 3.9.0, fixed in 3.9.1 in early Feb 2022. https://github.com/prisma/prisma/issues/11570

"prisma db pull doesn't read .env file and errors with Environment variable not found: DATABASE_URL"

cweekly
  • 8,706
  • 1
  • 22
  • 17
0

If you try with a schema completed and an empty db, you have this error. Try "prisma db push" first and after verify with "prisma studio".

TL CDAD
  • 1
  • 1
0

In my case I encountered a weird problem with the .env file itself, I created the file using Powershell's echo. Apparently, manually creating it in Vscode solves the problem.

Xiel
  • 21
  • 4
0

I was working in a monorepo, and found that my turbo rollup task didn't allow me to pass in flags. My work around was to modify my package.json by copying the .env to the same directory as schema.prisma file by adding a new command to hard reload with the flag I needed. I ended up adding this to my package.json

{
 ...
 "scripts": {
   ...
   "db-push:hard": "cp .env prisma/.env && yarn prisma db push --accept-data-loss",
   ...
 },
 ...
}   
haron68
  • 741
  • 1
  • 5
  • 19