RIP Nodemon… Or not?

Photo by Igor Son on Unsplash

RIP Nodemon… Or not?

alternative built in tool in Node.js

I was working on this Nest.js project, and after a while, I got annoyed by having to restart the server manually using the Ctrl+C command and then retyping the start command (luckily, I just used the up arrow). Fortunately, I remembered a package I used in an online web boot camp. I looked for it and found it on the first search result: Nodemon. As I tried to figure out how to use it, I came across a YouTube video by James Q Quick titled: I Don't Use Nodemon Anymore - I Do This Instead!.

So I decided to share with you this quick tip so you don’t have to go through the struggle anymore.

How does the auto-restart tool work?

We are going to start by understanding how nodemon (or any similar tool) automatically restarts your server or just your Java script file.

The idea is that the tool keeps an eye on certain files (you can precise which files) and whenever a change occurs within any of these files, (or a save button is triggered) the tool restarts the main file.

In the case of nodemon, instead of running your index.js for example, use the command: node index.js. You can use nodemon with the following command:nodemon index.js.

Nodemon monitors the files in the current work directory, if you want to change this behavior, you can use the — watch option to specify the file paths you want nodemon to monitor. For example, monitoring a server directory: nodemon --watch server.

You can learn more about nodemon in the documentation.

Node v18.11.0 watch mode

Node.js posted this tweet to find out that nodemon is one of the most useful tools for JavaSript users.

So, Node.js developers went ahead and added a cool new feature to Node v18.11.0+ that replaces nodemon. Now, you can start Node.js in watch mode by using the --watch option. For instance, if you want to keep an eye on a file named index.js, just use this command: node --watch index.js.

By default, the watch mode will monitor your current working directory and any required or imported modules. If you want to specify which paths to watch, just use --watch-path. However, keep in mind that this will disable the watching of required or imported modules, even when combined with --watch. For example, if you want to monitor only the files in the src and tests directories, use this command instead:

node --watch-path=./src --watch-path=./tests index.js

Nodemon can still be useful

Although most of Nodemon's features are provided by Node.js' watch mode, you can still use Nodemon to execute and monitor other programs, such as Python scripts: nodemon --exec python3 hello.py. More details can be found here.

Note that there are PyPi packages, like py-mon, that automatically restart your Python projects.

Conclusion

So yeah, the bottom line is, if you're looking to save time and make your workflow more efficient, auto-restart tools are the way to go.

Thank you for reading this blog post. By the way, this was the first one I have ever published. I would be grateful for your feedback on this post.