Forget nodemon, use watch mode instead in your Node apps

Irving Juárez
3 min readOct 19, 2022

--

Since Node 18.11.0, there is a built-in feature that allows us to watch every time a file is updated in our Node applications.

Index

  • What is and why we use nodemon for?
  • Introducing watch mode
  • Conclusion

What is and why we use nodemon for?

Nodemon is what it’s called a demon in Linux. A demon in a nutshell, is a program that re-start our app if it detects some changes in the code. For sure you need to save the file to the demon to detect the changes.

In the specific case of Node, nodemon allows us to re-run our app every time we make some changes.

This library was not made from Node.js creators, is a project apart. But it has been widely used to create Node apps in terms of development environment. Even it has +24k stars on github at the moment to writing this article.

However, Node just released a new version where nodemon could become unnecessary.

Introducing watch mode

Since v18.11.0 of Node.js, watch mode is available right out of the box, and as you can imagine, the use of this feature is very easy to use.

Imagine you have the following js file.

In order to use watch mode, you need to run the following command: node --watch index.js. And the program will keep running.

Now, every time you change something in the file, for example, the message, the program will re-run automatically again.

Interesting features

Watch mode provides with some interesting features that are not so obvious or that would require additional configurations in other demons.

The main feature is that watch mode listens to changes not only in the main file (in this case index.js), but in sub-files as well.

Consider both files are in the same dir. Now, if we make some changes in second.js, our program will be re-run again.

Therefore, watch node listen not only to the file where we are running the command, but to their dependencies as well.

Conclusion

Watch mode, at the moment to write this article is in experimental phase. So you can use it, but it might be buggy.

Watch mode main purpose is to be used in development environment, therefore, it’s not necessary to rush to send Node v18.11.0 into production.

--

--