prisma - getting environment variable not found error message when running graphql query
Asked Answered
K

14

38

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:[email protected]:5432/postgres"
Knelt answered 1/6, 2021 at 21:29 Comment(0)
K
121

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

Knelt answered 1/6, 2021 at 22:19 Comment(5)
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.Maffick
If you are a next.js user, then see the answers belowErebus
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.Godson
@Maffick env values don't need to be quotedWrit
My issue was actually having the value quoted, I removed them and worked fine.Slemmer
T
55

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.

Twinge answered 6/1, 2022 at 13:52 Comment(1)
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.Rosamondrosamund
E
47

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

Eliciaelicit answered 21/4, 2022 at 15:29 Comment(0)
L
2

if you are using windows just try to rename .env.local to .env This worked for me

Lorrielorrimer answered 7/1 at 14:27 Comment(0)
B
1

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.

Billposter answered 4/10, 2022 at 9:16 Comment(0)
K
1

If you are experiencing this issue and using Vercel Postgres DB, you may have tried to pull in your environment variables using vercel pull. This will only pull environment vars into your .env.development.local file. Prisma apparently will only be able to see env vars in .env, not any other dotfiles (unless you're using some other package like dotenv).

If you copy your environment vars from .env.development.local to .env and try running prisma commands (e.g. npx prisma db pull, npx prisma generate, npx prisma migrate) they should work now.

Koffler answered 19/4 at 15:54 Comment(0)
D
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"

Durwood answered 14/2, 2022 at 23:33 Comment(0)
H
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".

Hurryscurry answered 13/9, 2022 at 11:18 Comment(0)
C
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",
   ...
 },
 ...
}   
Cloche answered 10/8, 2023 at 5:19 Comment(0)
I
0

it works for me I have the same issue I just ran npx prisma db push then everything works fine

Indeliberate answered 3/12, 2023 at 5:42 Comment(0)
W
0

For me it was because i was using '@prisma/client/edge'

import { PrismaClient } from '@prisma/client/edge'

but actually, we need to use @prisma/client

import { PrismaClient } from '@prisma/client'
Woo answered 19/2 at 17:32 Comment(0)
P
0

Go to the folder where your migrations exist and then run the command

npx prisma migrate dev --name name_of_migration_file
Pentaprism answered 21/2 at 10:8 Comment(0)
B
0

I removed the Postgres installed on my Windows completely, and it worked.

Belkisbelknap answered 11/5 at 21:50 Comment(0)
S
0

I solved the problem like this:

Step 1:

cp .env.local .env

(If your code was in .env.local, this will generate a .env file with the settings)

Step 2: (if you are using docker) now you can run:

docker compose up

or if docker has one more file you can run it like this:

docker-compose -f docker-compose.dev.yml up
Send answered 15/5 at 22:41 Comment(2)
Please use spacing and try to add code in code blocks for easy readability. I am not able to submit edit. However it looks like that you are able to solve this problemUrbano
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Urbano

© 2022 - 2024 — McMap. All rights reserved.