Skip to content

How to Replace nodemon with Native Node.js Watch

Purpose

This post demonstrates how to replace nodemon with native Node.js watch features for better performance.

Environment

  • Node.js 18+
  • npm or yarn
  • Basic Node.js project

The Problem

When I run my development server with nodemon, I got this:

Terminal window
$ npm run dev
> nodemon --exec node --inspect src/index.js
[nodemon] 3.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,json,mjs,cjs
[nodemon] starting `node --inspect src/index.js`
Server running on port 3000

The issue is nodemon uses a lot of memory and restarts are slow. I got tired of waiting for nodemon to restart my server on every file change.

What I Was Doing

I was running a simple Express.js application and noticed nodemon was using 200MB of RAM just for watching files. The restart delays were affecting my productivity.

Here’s my setup:

package.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"dev": "nodemon --exec node --inspect src/index.js",
"start": "node src/index.js
},
"dependencies": {
"express": "^4.18.2
},
"devDependencies": {
"nodemon": "^3.0.3
}
}

I can explain the key parts:

  • "dev" script uses nodemon to watch for changes
  • --exec node tells nodemon to run node command
  • --inspect enables debugging

But when nodemon restarts, it takes 2-3 seconds and consumes lots of resources.

How to Solve It

I tried to remove nodemon dependency first:

Terminal window
npm uninstall nodemon

But then I got this error:

Terminal window
$ npm run dev
> nodemon --exec node --inspect src/index.js
sh: nodemon: command not found

So I needed a different approach. I tried using native Node.js watch features:

package.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"dev": "node --watch --inspect src/index.js",
"start": "node src/index.js
},
"dependencies": {
"express": "^4.18.2
}
}

Now test again:

Terminal window
$ npm run dev
> node --watch --inspect src/index.js
Debugger attached.
Server running on port 3000

You can see that I succeeded to run the server with native watch features.

Advanced Configuration

I wanted to watch specific directories, not just everything:

package.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"dev": "node --watch src --watch tests --inspect src/index.js",
"start": "node src/index.js
},
"dependencies": {
"express": "^4.18.2
}
}

This watches only the src and tests directories, which is more efficient.

The Reason

I think the key reason for the performance improvement is:

  • Native Node.js watch is built into the runtime, no extra process
  • Better integration with the Node.js event loop
  • Less overhead from monitoring file systems
  • No need for additional dependencies

Summary

In this post, I showed how to replace nodemon with native Node.js watch features. The key point is that you can reduce dependencies and improve performance while maintaining basic file watching functionality.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments