process.loadEnvFile() does not override existing environment variables
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- JavaScript
- Estrellas
- 122k
- Forks
- 37.3k
- Merge medio
- 4 d 2 h
- PR fusionados (30 d)
- 283
Descripción
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?
- Create two files:
.envwithKEY=originaland.env.testwithKEY=override. - 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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el comportamiento con process.loadEnvFile() en el REPL de Node.js usando los dos archivos .env descritos en el issue. Lee la API de process.loadEnvFile() y su gestión existente de las variables de entorno; se considera terminado cuando una opción de override documentada pueda reemplazar los valores existentes sin modificar el comportamiento predeterminado, y haya pruebas que cubran cargas repetidas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript
- Área
- api, backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 52/100