Reputation: 3774
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:
This is what I have inside my .env
file looks like -
DATABASE_URL="postgres://postgres:[email protected]:5432/postgres"
Upvotes: 44
Views: 87110
Reputation: 1
Got the same error , but i was on a server , simply had to generate the client "npx prisma generate"
Upvotes: 0
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
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
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
Reputation: 569
I removed the Postgres installed on my Windows completely, and it worked.
Upvotes: 0
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
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
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
Reputation: 2055
if you are using windows just try to rename .env.local to .env This worked for me
Upvotes: 2
Reputation: 11
it works for me I have the same issue I just ran npx prisma db push then everything works fine
Upvotes: 1
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
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
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
Reputation: 1540
I had this issue in my NextJs project. after changing the .env.local file to .env everything worked.
Upvotes: 51
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
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
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