nodejs / nodejs/node

--watch prints messages to stdout, but should print to stderr

Open Beginner friendly
#56,478 4 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

v22.12.0

Platform
macOS (but reading the source code of Node.js the issue should be present on any platform)
Subsystem

node/lib/internal/main/watch_mode.js

What steps will reproduce the bug?
echo 'process.stdin.resume()' >index.mjs
node --watch index.mjs 2>/dev/null

In another terminal:

touch index.mjs
How often does it reproduce? Is there a required condition?

Always.

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

Nothing is printed, since I redirect stderr to /dev/null.

What do you see instead?

Restarting 'index.mjs' is printed (to stdout).

Additional information

How to use stdout and stderr is purely conventional, but the GNU C library says:

stdout: The standard output stream, which is used for normal output from the program.

stderr: The standard error stream, which is used for error messages and diagnostics issued by the program.

I’m making a program that reads input from stdin and writes to stdout. It is a long-lived program: Another program writes to stdin over time, and consumes output from stdout. I use --watch so I can iterate on the sub-program more easily. Unfortunately, --watch writes messages to stdout, causing the consuming program to receive output it does not expect.

I would argue that my program creates the “normal output” while the --watch messages are “diagnostics”.

Here’s one of the places where stdout is used instead of stderr: https://github.com/nodejs/node/blob/9400eae52ee10f25d63c06c52479c033e3149580/lib/internal/main/watch_mode.js#L139

Is there a reason for this? Should it be changed to stderr?

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 in lib/internal/main/watch_mode.js, especially the stdout use near the linked line, and run the reported node --watch index.mjs reproduction with stderr redirected. Done means watch-mode messages such as "Restarting 'index.mjs'" go to stderr so they are suppressed by 2>/dev/null and do not interfere with the program's stdout.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.