The short answer

#

Run your entry file with node --watch. Node.js restarts the process whenever that file or any module it imports changes. Watch mode has been stable since Node.js 22.0.0, so on any current release you can drop nodemon for most projects.


_10
{
_10
"scripts": {
_10
"dev": "node --watch src/server.js"
_10
}
_10
}

Why it happens

#

Node.js loads your code into memory once when the process starts. Editing a file on disk does not change the code that is already running, so without a watcher you have to stop and start the process by hand.

nodemon solved this by watching your folders and restarting Node.js for you. Watch mode builds the same idea into Node.js itself: it tracks the files your app actually loads through import and require, and when one changes it stops the process and starts a fresh one.

How to fix it

#

Use --watch for the entry file and its imports

This is all most servers need. Only files that are part of the module graph trigger a restart, so editing a README or a test file does not restart your server.


_10
node --watch src/server.js

Node.js options go before the file name. Anything after the file name is passed to your script as an argument.

Use --watch-path for files you read yourself

Files you read with fs at runtime, such as templates, JSON config read with readFile, or SQL files, are not imports, so --watch does not see them. Point watch mode at folders explicitly with --watch-path. You can repeat the flag. Note that using --watch-path turns off the automatic watching of imported modules, so include your source folder too.


_10
node --watch-path=./src --watch-path=./templates src/server.js

Check the --watch-path section of the CLI docs for your Node.js version, as its stability level and platform notes differ from plain --watch.

Keep your terminal output between restarts

Watch mode clears the console on each restart. Add --watch-preserve-output if you want to scroll back through earlier logs.


_10
node --watch --watch-preserve-output src/server.js

Combine it with other built-in features

Watch mode works alongside other flags, so a typical dev script can load environment variables and restart on change with no extra packages.


_10
{
_10
"scripts": {
_10
"dev": "node --watch --env-file=.env src/server.js",
_10
"test:watch": "node --test --watch"
_10
}
_10
}

Shut down cleanly on restart

On each restart Node.js signals the old process to stop. If you hold resources such as a database pool or an open port, listen for the signal and close them, so the new process does not hit EADDRINUSE.


_10
process.on('SIGTERM', async () => {
_10
await pool.end();
_10
server.close(() => process.exit(0));
_10
});

When you still need another tool

nodemon or a similar watcher is still useful if you need to run a command that is not Node.js, add a delay before restarting, or run custom actions on change. Bundler-based frameworks such as Next.js or Vite have their own dev servers with hot reload, so use those rather than watch mode.

Common pitfalls

#
  • Putting --watch after the file name, where it becomes an argument to your script.
  • Expecting changes to .env or files read with fs to trigger a restart without --watch-path.
  • Using watch mode in production. It is a development tool; use a process manager or your platform's restart policy instead.
  • Leaving long-running timers or open handles that delay the old process exiting, which makes restarts feel slow.
#
file under:
Node.js

quick question

Was this resource helpful?