nodejs / nodejs/node

process.loadEnvFile() does not override existing environment variables

Aperta
#63,027 4 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

dotenv feature request
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

Version

v24.14.0

Platform
Darwin telsha 25.4.0 Darwin Kernel Version 25.4.0: Thu Mar 19 19:31:09 PDT 2026; root:xnu-12377.101.15~1/RELEASE_ARM64_T8132 arm64
Subsystem

No response

What steps will reproduce the bug?
  1. Create two files: .env with KEY=original and .env.test with KEY=override.
  2. Run the following in the Node.js REPL:
// Load the first file
process.loadEnvFile(".env");
console.log(process.env.KEY); // 'original'

// Attempt to load the second file to override the value
process.loadEnvFile(".env.test");
console.log(process.env.KEY); // Still 'original'

Mitigation:

import { parseEnv } from 'node:util';
import { readFileSync } from 'node:fs';

// This forces the override
const integConfigs = parseEnv(readFileSync("apps/server/.env.integ", "utf8"));
Object.assign(process.env, integConfigs);
How often does it reproduce? Is there a required condition?

It reproduces 100% of the time. The condition is that a variable must already exist in process.env (either from the shell or a previous loadEnvFile call) for the "bug" to be visible.

What is the expected behavior? Why is that the expected behavior?

While the default behavior of not overriding is standard for dotenv, there is currently no programmatic way (via options) to force an override using process.loadEnvFile().

At the very least, subsequent calls to process.loadEnvFile() should support an { override: true } option to allow developers to switch environment contexts (e.g., switching to an integration/test config) without manually parsing files via util.parseEnv.

What do you see instead?

The variable remains locked to the first value it was assigned. Subsequent calls to loadEnvFile for the same key are silently ignored for that specific key.

Additional information

Most modern environment loaders (like the dotenv npm package) provide an override flag. Without this, process.loadEnvFile is significantly less useful for complex test runners or mono-repos where multiple .env files must be composed or swapped during a single process lifetime.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il comportamento con process.loadEnvFile() nella REPL di Node.js usando i due file .env descritti nell’issue. Leggi l’API di process.loadEnvFile() e la gestione esistente delle variabili d’ambiente; il lavoro è completato quando un’opzione di override documentata può sostituire i valori esistenti preservando il comportamento predefinito, con test che coprono caricamenti ripetuti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
api, backend
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.