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
--watchflag (stable since Node 20) is Node's built-in file-watching restart mechanism —node --watch server.jsgenuinely watches the running file (and itsrequire/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.jsprocess, 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 (3988vs. the original31688) — 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
--watchgenuinely 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-runningnode server.jsyourself, just automated. --watchalso 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--watchbehavior verified above.- The honest, precise comparison to
nodemon:--watchcovers 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--watchfirst 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.readFileSyncrather thanrequire, for instance)?" — directly relevant to whether--watch-pathis 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
--watchflag, or that--watchgenuinely 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.