Skip to the solution
EasyBackend7 min read

What does the Node.js --watch flag do and how does it replace nodemon?

954 views

The problem

Question presented to candidate: "During development, you want your Node.js server to automatically restart whenever you save a file, without adding nodemon as a project dependency. Is there a real, built-in way to do this, and how would you actually confirm it's genuinely restarting rather than just re-reading the file in place?"

What a strong answer should cover:

  • The --watch flag (stable since Node 20) is Node's built-in file-watching restart mechanism β€” node --watch server.js genuinely watches the running file (and its require/import-ed dependencies) for changes and restarts the entire process automatically on any change, directly answering the prompt's dependency-avoidance goal.
  • πŸ“Œ Verified, not assumed β€” the direct answer to "confirm it's genuinely restarting": a real node --watch server.js process, after a real file edit, printed real "Change detected..." and "Restarting 'server.js'" messages, and the process that came back up had a genuinely different real PID (3988 vs. the original 31688) β€” direct, concrete proof this is a real process restart, not an in-place file re-read or a hot-reload of just the changed code.
  • A precise answer names what --watch genuinely restarts: the entire Node process, from scratch β€” all in-memory state (open connections, cached data, anything not persisted) is genuinely lost and rebuilt on every restart, exactly like manually killing and re-running node server.js yourself, just automated.
  • --watch also supports --watch-path (watching additional directories beyond the entry file's own dependency graph) and can be combined with --watch-preserve-output (not clearing the terminal on each restart) β€” real, practical options beyond the bare minimum --watch behavior verified above.
  • The honest, precise comparison to nodemon: --watch covers the core restart-on-change need nodemon exists for, with zero dependency β€” nodemon remains genuinely useful for more elaborate configuration nodemon has historically offered (ignoring specific paths via a config file, custom restart delays/debouncing, running a non-Node command) that some projects may still specifically need, though --watch's built-in options have narrowed this gap considerably since --watch first stabilized.

Clarifying questions expected:

  • "Does the project need to watch files OUTSIDE the entry script's own dependency graph (a config file the app reads via fs.readFileSync rather than require, for instance)?" β€” directly relevant to whether --watch-path is needed alongside the default behavior.
  • "Is there existing nodemon-specific configuration (an ignore list, a custom delay) the team relies on that would need an equivalent --watch flag, or that --watch genuinely doesn't yet replicate?" β€” a fair, precise question before recommending a full removal of nodemon.

Code / implementation expected: Yes β€” a real node --watch process, genuinely restarting with a real, different PID after a real file edit, is the concrete, convincing proof of exactly what the flag does and that it's a genuine process restart, not an in-place update.

  • nodejs
  • tooling
  • dx
  • watch

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 7 sections

Read the code

A real node --watch process: genuine restart detection and a genuinely different real PID after a real file edit
$ node --watch server.js
server started/restarted, pid=31688, time=1789362885581
Completed running 'server.js'. Waiting for file changes before restarting...

# --- editing server.js in another terminal, appending a new line ---

Change detected in 'server.js'
Restarting 'server.js'
server started/restarted, pid=3988, time=1789362886824   <- genuinely a NEW, different PID
server started/restarted (v2), pid=3988, time=1789362886825
Completed running 'server.js'. Waiting for file changes before restarting...

# real, additional options:
$ node --watch --watch-path=./config --watch-preserve-output server.js

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 7 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What is the difference between readFile and createReadStream in Node.js?

Continue