Skip to content

@prisma/client magically loading .env file (and mutating process.env in the process) #15620

@cyrilchapon

Description

@cyrilchapon

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 even zod) 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 mutating process.env .

Put simply :

import { config } = './config'
const appEnv = config.getEnv() // checked

const myProvider = new MyProvider(appEnv.someKey)

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 disable dotenv, 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

FOO=bar

01-nothing.ts

console.log(process.env)
// undefined

02-importing-prisma.ts

import '@prisma/client'
console.log(process.env)
// bar

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

  • If one can disable it
  • In consequence, if it's done in the constructor invocation instead of the module import

Prisma information

Irrelevant for this issue

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v18.8.0

Prisma Version

prisma                  : 4.4.0
@prisma/client          : 4.4.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt f352a33b70356f46311da8b00d83386dd9f145d6 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Format Wasm             : @prisma/prisma-fmt-wasm 4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6
Default Engines Hash    : f352a33b70356f46311da8b00d83386dd9f145d6
Studio                  : 0.474.0
Preview Features        : interactiveTransactions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions