nodejs / nodejs/node

process.loadEnvFile() does not override existing environment variables

Đang mở
#63,027 4 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

dotenv feature request
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.4k
Merge trung bình
4 ngày 3 giờ
Pull request đã merge (30 ngày)
272

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện hành vi với process.loadEnvFile() trong Node.js REPL bằng hai tệp .env được mô tả trong issue. Đọc API của process.loadEnvFile() và cách xử lý biến môi trường hiện có; công việc được xem là hoàn tất khi một tùy chọn override được ghi rõ trong tài liệu có thể thay thế các giá trị hiện có mà vẫn giữ nguyên hành vi mặc định, với các bài kiểm thử bao quát việc tải lặp lại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript
Lĩnh vực
api, backend
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.