Sumchans
Sumchans

Reputation: 3774

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

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"

Upvotes: 44

Views: 87110

Answers (17)

Got the same error , but i was on a server , simply had to generate the client "npx prisma generate"

Upvotes: 0

Simone Tasca
Simone Tasca

Reputation: 21

for me the problem was that i passed undefined to datasourceUrl in the PrismaClient contstructor so it used as fallback the environment variable set in the schema.prisma

Upvotes: 0

Shuvo
Shuvo

Reputation: 177

If you are using nextjs with prisma and stored your env virables in .env.local or something else which is not exact .env file, then you have to load that env file manually.

for my case, I have used dot env cli with custom scripts in package.json. Check the official prisma docs for env

 "scripts": {
    ....,
    "migrate:reset": "dotenv -e .env.local -- pnpx prisma migrate reset",
    "migrate:dev": "dotenv -e .env.local -- pnpx prisma migrate dev"
  },

Upvotes: 0

João Viitor
João Viitor

Reputation: 17

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

Upvotes: 0

Collaxd
Collaxd

Reputation: 569

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

Upvotes: 0

ctlockey
ctlockey

Reputation: 352

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.

Upvotes: 2

Shyam Raghuwanshi
Shyam Raghuwanshi

Reputation: 21

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

npx prisma migrate dev --name name_of_migration_file

Upvotes: 0

knock
knock

Reputation: 1

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'

Upvotes: 0

Vahap Gencdal
Vahap Gencdal

Reputation: 2055

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

Upvotes: 2

ARUN KUMAR
ARUN KUMAR

Reputation: 11

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

Upvotes: 1

haron68
haron68

Reputation: 791

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",
   ...
 },
 ...
}   

Upvotes: 0

Xiel
Xiel

Reputation: 61

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.

Upvotes: 1

TL CDAD
TL CDAD

Reputation: 1

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".

Upvotes: 0

elyas.m
elyas.m

Reputation: 1540

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

Upvotes: 51

cweekly
cweekly

Reputation: 9075

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"

Upvotes: 0

Pecata
Pecata

Reputation: 1063

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.

Upvotes: 62

Sumchans
Sumchans

Reputation: 3774

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

Upvotes: 131

Related Questions