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


Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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"

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 azium
Solution 2
Solution 3 cweekly
Solution 4 elyas.m