41

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"

15 Answers 15

124

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

5
  • 3
    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. Commented Jan 4, 2022 at 21:54
  • 3
    If you are a next.js user, then see the answers below Commented Jun 21, 2023 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
    Commented Jul 3, 2023 at 7:18
  • 1
    @aproximation env values don't need to be quoted
    – Madbreaks
    Commented Jan 19 at 22:26
  • My issue was actually having the value quoted, I removed them and worked fine.
    – ncardez
    Commented Jul 7 at 10:44
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.

1
  • 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
    Commented Oct 12, 2022 at 3:46
49

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

0
2

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

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.

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.

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"

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

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

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

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'
0

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

npx prisma migrate dev --name name_of_migration_file
0

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

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
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 problem Commented May 17 at 8:58
  • 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. Commented May 17 at 8:59
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.