Skip to content

importing prisma client pollutes process.env without using dotenv #19519

Closed

Description

@jacobakkerboom

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

Activity

  1. added
    bug/1-unconfirmed
    on Jun 30, 2023
  2. ostkrok commented on Jul 20, 2023

    @ostkrok

    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 .env containing FLAG_A=1.
    And we have a test suite that loads .env.test1 with nothing in it.
    The test would now run with FLAG_A=1 even 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=0 or such, in the .env we load ourselves, but it doesn't work if we want to unset it.

  3. self-assigned this
    on Jul 20, 2023
  4. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Jul 21, 2023
  5. Jolg42 commented on Jul 21, 2023

    @Jolg42
    Contributor

    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 .env file 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

  6. mnahkies commented on Apr 29, 2024

    @mnahkies

    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 .env instead of test.env which they normally use (basically the prisma import started executing before our code called dotenv.config, and we weren't passing override: true so the second dotenv.config with the correct config was being ignored)

    Eventually I tracked it down to prisma by installing a Proxy around process.env and using console.trace() on the writes to it.

    This scares the hell out of me - why is prisma loading 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 .env files 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 prisma I find the debugging process quite painful due to the use of bundled/minified files like @prisma/client/runtime/library.js - do we really need our node_modules minified?

    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.

  7. mnahkies commented on Apr 29, 2024

    @mnahkies

    To add to my previous comment, it seems the exact place this happens is when this piece of code from @prisma/client/index.js is executed, which occurs during module import (essentially causing the act of importing the prisma client to have side effects)

    const { warnEnvConflicts } = require('@prisma/client/runtime/library.js')
    
    warnEnvConflicts({
        rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(config.dirname, config.relativeEnvPaths.rootEnvPath),
        schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(config.dirname, config.relativeEnvPaths.schemaEnvPath)
    })
    

    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 warnEnvConflicts to not modify process.env at least?

  8. janpio commented on Apr 30, 2024

    @janpio
    Contributor

    That is a good suggestion we would certainly consider merging @mnahkies (if it really has no adverse effects on current usage).

  9. removed their assignment
    on Jun 11, 2024
  10. added this to the 7.0.0 milestone on Nov 3, 2025
  11. aqrln commented on Nov 3, 2025

    @aqrln
    Member

    Closing as the PR that completely resolves this issue obsolete has been merged to the next branch for Prisma 7.

    Note also that using the new prisma-client generator in Prisma 6 already doesn't pollute process.env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions