6

I am working on a nestjs project using Prisma, and I want to use multiple .env files with Prisma. I follow the guide here. According to the guide, I add:

"migratetest:mysql": "dotenv -e .env.test -- npx prisma migrate dev",
"migratedev:mysql": "dotenv -e .env.development -- npx prisma migrate dev"

to my package.json. I run migratetest:mysql to load the .env.test file and do the migration. Then, I run start: dev to start the app. However, the Prisma said:

Error: error: Environment variable not found: DATABASE_URL.
  -->  schema.prisma:10
   | 
 9 |   provider = "mysql"
10 |    url      = env("DATABASE_URL")
   | 

It seems that it can not find the .env file in my project (based on the guide, there is no .env file, it should change to .env.test and .env.development)

here is my .env.test:

DATABASE_URL=mysql://root:123456@localhost:3306/test

here is my .env.development:

DATABASE_URL=mysql://root:123456@localhost:3306/dev

please help :)

Erika
  • 453
  • 8
  • 23

1 Answers1

9

You need to tell every script you have to use dotenv cli, so your start:dev should look like this:

"start:dev": "dotenv -e .env.development nest start --watch",

However, for me it's not starting the compilation in watch mode, I'm still trying to debug this. Any ideas?

EDIT: I posted this and 2 minutes later I was able to solve my issue :D The final version of the script should look like this:

"start:dev": "dotenv -e .env.development -- nest start --watch",

Reason for the need of two dashes being explained in Prisma docs:

Note: dotenv doesn't pass the flags to the Prisma command by default, this is why the command includes two dashes -- before prisma, making it possible to use flags like --force, --schema or --preview-feature.