Skip to content

--env-file-if-exists throws error when the .env file doesn't exist and when combined with --watch (now in lts) #57040

@mkvlrn

Description

@mkvlrn

Version

v22.14.0
v24.13.1, v25.6.1

Platform

Linux arch 6.13.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 08 Feb 2025 18:54:55 +0000 x86_64 GNU/Linux

Linux archlinux 6.18.9-zen1-2-zen #1 ZEN SMP PREEMPT_DYNAMIC Mon, 09 Feb 2026 17:45:06 +0000 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run any script in a project where a .env file doesn't exist:

mkdir repro && cd repro && npm init -y && echo "console.log('repro');" > main.js
node --env-file-if-exists=.env --watch main.js

If the .env file exists or you don't use the --watch flag, script runs normally without throwing errors.

How often does it reproduce? Is there a required condition?

These need to be true for the problem to occur:

  • use the flag --env-file-if-exists pointing to a non existing .env file, like --env-file-if-exists=.env
  • use the flag --watch

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

Script should run normally with the following message being printed right before it:

.env not found. Continuing without it.

What do you see instead?

An error stating .env was not found.

.env not found. Continuing without it.
node:internal/fs/watchers:247
    const error = new UVException({
                  ^

Error: ENOENT: no such file or directory, watch '/home/mkvlrn/sandbox/repro/.env'
    at FSWatcher.<computed> (node:internal/fs/watchers:247:19)
    at watch (node:fs:2550:36)
    at FilesWatcher.watchPath (node:internal/watch_mode/files_watcher:109:21)
    at FilesWatcher.filterFile (node:internal/watch_mode/files_watcher:128:12)
    at start (node:internal/main/watch_mode:78:13)
    at init (node:internal/main/watch_mode:148:15)
    at node:internal/main/watch_mode:160:1 {
  errno: -2,
  syscall: 'watch',
  code: 'ENOENT',
  path: '/home/mkvlrn/sandbox/repro/.env',
  filename: '/home/mkvlrn/sandbox/repro/.env'
}

Node.js v22.14.0
.env not found. Continuing without it.
repro
node:events:496
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at target._send (node:internal/child_process:877:20)
    at target.send (node:internal/child_process:751:19)
    at reportModuleToWatchMode (node:internal/modules/cjs/loader:279:13)
    at Function._load (node:internal/modules/cjs/loader:1097:5)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49
Emitted 'error' event on process instance at:
    at node:internal/child_process:881:39
    at process.processTicksAndRejections (node:internal/process/task_queues:85:11) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v22.14.0

Additional information

This is almost a 100% copy of a previous issue (#56887) I had opened a couple weeks back, but that issue was found on the latest/23 version, while not present in the then current LTS v22.13.1. Now it is happening in the LTS 22.14 version. Still working fine in 22.13.1.

22.13.1
Image

22.14
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions