nodejs / nodejs/node

process.loadEnvFile() does not override existing environment variables

オープン
#63,027 コメント 4 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

dotenv feature request
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、issue に記載されている 2 つの .env ファイルを使い、Node.js REPL で process.loadEnvFile() による動作を再現します。process.loadEnvFile() API と既存の環境変数処理を読みます。完了条件は、デフォルトの動作を維持しながら既存の値を置き換えられる、文書化された override オプションを用意し、繰り返し読み込むケースをテストでカバーすることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
api, backend
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。