Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 334
Sumchans Avatar asked Jun 01 '21 21:06

Sumchans


People also ask

Can you have more than one .ENV file?

One solution is to have multiple . env files which each represent different environments. In practice this means you create a file for each of your environments: .

What is the use of .ENV file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

Do not commit environment variables in Prisma?

No do not commit environment variables. It needs to be added to the README.md Got it, also it should be on the root of the project, correct? Yep! Can you also create a .env in the prisma folder with just the DATABASE_URL? Can I update the README.md with this information and put in a PR? Or is there sensative information that we shouldn't use?

What are the most common errors in graphiql?

variables and fragments not defined properly. This is a typical error that you get when a client makes a request with a variable that has not been defined. A common nightmare when using GUI tools like GraphiQL where the variables UI has remnants of variable usage from previous queries.

Do not commit environment variables to the project folder?

No do not commit environment variables. It needs to be added to the README.md Got it, also it should be on the root of the project, correct? Yep! Can you also create a .env in the prisma folder with just the DATABASE_URL?

How to understand query validation rules in GraphQL-JS server?

Read the source code for validation rules in graphql-js server to understand various query validation that goes through before executing the query. Do note that validation errors never return any data to the client, the execution errors have the ability to return partial data sets.


Video Answer


3 Answers

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

like image 97
Sumchans Avatar answered Oct 24 '22 03:10

Sumchans


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.

like image 31
Pecata Avatar answered Oct 24 '22 02:10

Pecata


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

like image 2
elyas.m Avatar answered Oct 24 '22 04:10

elyas.m