nodejs / nodejs/node

--watch-path restarts on unrelated directory touch when --env-file is set

Open
#61,906 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

watch-mode
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version
  • Node.js: v24.13.0
  • OS: macOS
Reproduction

Setup:

mkdir -p /tmp/node-watch-envfile-repro/watched /tmp/node-watch-envfile-repro/unrelated
cd /tmp/node-watch-envfile-repro

echo "FOO=bar" > .env

echo "console.log('START', JSON.stringify({ pid: process.pid, ts: Date.now(), foo: process.env.FOO ?? null }));" > watched/server.js
echo "setInterval(() => {}, 1000);" >> watched/server.js

Run with --env-file and touch an unrelated path:

cd /tmp/node-watch-envfile-repro

node --watch-path=watched --env-file=.env watched/server.js > out-env.log 2>&1 &
PID=$!

sleep 2
touch unrelated
sleep 2

kill $PID
cat out-env.log

Observed output:

START {"pid":58939,"ts":1771621917212,"foo":"bar"}
Restarting 'watched/server.js'
START {"pid":58948,"ts":1771621919442,"foo":"bar"}

Now run without --env-file:

cd /tmp/node-watch-envfile-repro

node --watch-path=watched watched/server.js > out-no-env.log 2>&1 &
PID=$!

sleep 2
touch unrelated
sleep 2

kill $PID
cat out-no-env.log

Observed output:

START {"pid":59279,"ts":1771621950778,"foo":null}
Expected behavior

When --watch-path=watched is set, touching unrelated should not trigger a restart, regardless of whether --env-file is used.

Actual behavior

With --env-file=.env, touching unrelated triggers a restart.
Without --env-file, touching unrelated does not restart.

Notes

This was first noticed in a larger workspace where:

node --watch-path=apps/server/dist-dev --env-file=.env apps/server/dist-dev/server.js

restarts when running:

touch apps/client/dist/

The reduced test case above reproduces the same behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied macOS reproduction with and without --env-file, focusing on Node.js watch-path handling and the interaction with the --env-file entry point. Trace where filesystem paths are selected for watching and add or update a regression test so touching unrelated does not restart the process; rerun the reproduction and relevant watch tests to confirm the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.