Repository navigation
Closed
Labels
bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.topic: .envtopic: breaking changetopic: envtopic: environmenttopic: prisma-client
Milestone
Description
Bug description
If you put MY_TEST_VAR in a .env file and import a prisma client, then process.env.MY_TEST_VAR will be defined, despite the fact that we are not using e.g. dotenv.
How to reproduce
prisma/schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL_MAIN")
}
model Locations {
LocationID Int @id
LocationName String @db.VarChar(45)
}
package.json:
{
"type": "module",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"@prisma/client": "^4.14.1",
"prisma": "^4.14.1"
}
}.env:
MY_TEST_VAR="THIS IS THE VALUE OF MY_TEST_VAR"app.js:
import { PrismaClient } from '@prisma/client'
console.log(process.env.MY_TEST_VAR);npx prisma generate
npm start
(logs the variable, not undefined)
Expected behavior
The expected behaviour is that undefined is logged
So what is said here is a bit misleading, because the variables will be loaded anyway
"If you want environment variables to be evaluated at runtime, you need to load them manually in your application code (for example, by using dotenv)"
Prisma information
(same as above)
prisma/schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL_MAIN")
}
model Locations {
LocationID Int @id
LocationName String @db.VarChar(45)
}
Environment & setup
Mac and linux
Prisma Version
prisma : 4.15.0
@prisma/client : 4.15.0
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine 8fbc245156db7124f997f4cecdd8d1219e360944 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine : migration-engine-cli 8fbc245156db7124f997f4cecdd8d1219e360944 (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Format Wasm : @prisma/prisma-fmt-wasm 4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944
Default Engines Hash : 8fbc245156db7124f997f4cecdd8d1219e360944
Studio : 0.484.0
Reacted by Scott Talbot, Kim Nilsson, Joël Galeran, Michael Nahkies, Aleksandr Beskrovniy, Daniel Mondragón, BONG-U SON and Cheeshian Chuah
Metadata
Metadata
Assignees
Labels
bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.topic: .envtopic: breaking changetopic: envtopic: environmenttopic: prisma-client
Activity
fix(env): Remove section about having to use dotenv to use env vars
ostkrok commentedon Jul 20, 2023
We've also noticed this behavior, and find it quite confusing and unhelpful.
It causes issues for us as we're running separate test suites with different .env files.
Let's say that we have a default
.envcontainingFLAG_A=1.And we have a test suite that loads
.env.test1with nothing in it.The test would now run with
FLAG_A=1even if we've never explicitly loaded that file.I don't really see any reason why a library should force such behavior.
Sometimes it's possible to work around by setting
FLAG_A=0or such, in the.envwe load ourselves, but it doesn't work if we want to unset it.Jolg42 commentedon Jul 21, 2023
Thanks for adding extra information, I can reproduce the behavior stated in the description.
I'm a bit scared to find out how many people actually depend on that behavior, as that would be a breaking change for them.
We're thinking about how to handle this.
In the meantime, I think not having any
.envfile would be a workaround, at the cost of needing to handle it yourself, with for example.env.dev.We have some docs that can help for that https://www.prisma.io/docs/guides/development-environment/environment-variables/using-multiple-env-files
mnahkies commentedon Apr 29, 2024
I've just lost a fair bit of time figuring this out 😞
I made a seemingly innocent change, which then caused my test suites to start loading
.envinstead oftest.envwhich they normally use (basically the prisma import started executing before our code calleddotenv.config, and we weren't passingoverride: trueso the seconddotenv.configwith the correct config was being ignored)Eventually I tracked it down to
prismaby installing aProxyaroundprocess.envand usingconsole.trace()on the writes to it.This scares the hell out of me - why is
prismaloading configuration implicitly like this? It should be up to the consuming application to load it's configuration from the correct sources IMO.It feels like the sort of trap that could easily result in someone unwittingly running their tests against a production database by accident, and cause an incident.
Sure there are probably people relying on it unwittingly, but it's the sort of breaking change that will be very visible and easy to fix (your application no longer connects to the database, staging deployment fails, you add dotenvx or whatever, though personally I steer clear of using
.envfiles outside of local development to start with). Feels like a major version bump and a short migration guide/advice would be fine?On a side note, any time I have issues with
prismaI find the debugging process quite painful due to the use of bundled/minified files like@prisma/client/runtime/library.js- do we really need ournode_modulesminified?I'd consider this a packaging concern for the application developer, where they can choose to bundle/minify their application for production if they wish, and leaving nice human readable source files present in development.
mnahkies commentedon Apr 29, 2024
To add to my previous comment, it seems the exact place this happens is when this piece of code from
@prisma/client/index.jsis executed, which occurs during module import (essentially causing the act of importing the prisma client to have side effects)It also occurs when you construct the
PrismaClient, but I think this is less of a problem, compared to the module import (though I would ideally prefer no implicit config loading).Might it be safe to alter
warnEnvConflictsto not modifyprocess.envat least?janpio commentedon Apr 30, 2024
That is a good suggestion we would certainly consider merging @mnahkies (if it really has no adverse effects on current usage).
feat!: remove env loading and dotenv support (#28298)
aqrln commentedon Nov 3, 2025
Closing as the PR that completely resolves this issue obsolete has been merged to the
nextbranch for Prisma 7.Note also that using the new
prisma-clientgenerator in Prisma 6 already doesn't polluteprocess.env.feat!: remove env loading and dotenv support (#28298)
feat!: remove env loading and dotenv support (#28298)
feat!: remove env loading and dotenv support (#28298)