New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@prisma/client
magically loading .env
file (and mutating process.env
in the process)
#15620
Comments
I agree with cyrilchapon here. The behavior should be optional. And it shouldn't be a side effect of simply importing the I had several occasions now where the silent loading of environment variables by only importing the One while writing tests (with custom environment variables) where the It would be great to be able to disable the automatic loading of the |
for the time being i am working around this by loading the .env first, then importing prisma using |
Just spent an hour trying to find out why my env variables were messed up. Prisma is clashing with nestJS here and I prefer to handle loading the env variables myself using nestjs. We really need to be able to turn this off |
In which context exactly did this mess with nestJS, Prisma Client in a NestJS app? How did the clash happen? An issue with a concrete request for an env var to disable the behavior might be a good idea then. |
Hi! My concrete issue was:
I can do additional testing on this next week and provide a minimal reproducible example together with a concrete issue/feature request. |
To expand on use-cases, and to get rid of any .env-related misperception, basic user story :
There are several use-cases where a .env is not relevant at all, including deploying on Vercel, developing in a containerized Node.js env, deploying on Heroku. I know : one should exclude its .env from got sources; and after-all dotenv keep actual environment variables precedence over .env files. But the fact is, in my opinion, that's not a viable reason to "try and load them anyway". Let's please keep in mind that at the root of the thoughts about "environment variables configuration" there is 12 factors; which is not about .env files at all. |
@prisma/client
magically loading .env
file (and mutating process.env
in the process)
You can find a minimal repro repo here: https://github.com/Mydayyy/nestjs-prisma-bug-repro The readme hopefully should cover the usage. Also there is already a related issue here: #18239 |
Ok, so it is correct that your NestJS app is conflicting with Prisma's behavior because NestJS loads environment variables from certain files in a certain order, which Prisma then messes with by its normal, defined behavior? |
Two more comments in another duplicate, now closed issue: |
My use case is the following: I have a I found this workaround which seems to do the trick: in my tests setup file, I mock the package vi.mock('@prisma/internals') Is it a good practice? Is there any downside I'm not aware of? |
The case I have is a prisma client in /apps/core which is reading the .env from /apps/core. This combined with another app in /apps/anotherApp. I have two variables in both /apps/core/.env and /apps/anotherApp/.env which are named the same. EXAMPLE="first_example" in /apps/core/.env and EXAMPLE="second_example" in /apps/anotherApp/.env. To fix this:
So now the prisma client can still have a .env file in /apps/core but the EXAMPLE variable is moved to /apps/core/environment/.env |
Bug description
On a fresh repository with just
prisma
and@prisma/client
installed, the fact of importing @prisma/client silently loads my local .env file without letting me any chance to disable this behavior.The variety of use-cases (#12535, #10104 and more generally the amount of dotenv-related issues ) appear to me as a signal that prisma is apparently trying to do too much when loading such a file.
My personal use-case is simple; I want to strongly validate environment format (with
envalid
or evenzod
) BEFORE doing anything else. I also want to provide my own configuration to my "providers" (and prisma is one of them) in the parsed format I want; without mutatingprocess.env
.Put simply :
I saw several strategies (docs/environment-variables, docs/using-multiple-env-files, docs/managing-env-files-and-setting-variables), and various possibilities through issues (like "use
dotenv-cli
before calling prisma, prisma and dotenv will be clever enough to detect you already loaded env"). Those are simply not enough for me and this use case. I want to entirely disabledotenv
, not only for development but even more for production.How to reproduce
Install a fresh repository, and add
@prisma/client
+prisma
.Create a sample schema, and
prisma generate
.env
01-nothing.ts
02-importing-prisma.ts
Expected behavior
I'd expect @prisma/client not parsing anything like a .env unless I tell it to do so.
Alternatively, I'd expect @prisma/client to accept me telling "don't load it, please".
In other words : the default of loading .env appears legit; but
Prisma information
Irrelevant for this issue
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: