10.9 C
New York
Saturday, January 13, 2024

Utilizing Nodemon and Watch in Node.js for Reside Restarts — SitePoint

Node.js growth may be slower than needed if it’s a must to cease and restart your new utility each time you make a change. This tutorial offers two options to enhance your coding workflow.

Desk of Contents

When you’ve ever developed a PHP utility, you’ll know you can also make updates and refresh your browser to check the adjustments. An online server corresponding to Apache or NGINX receives a your request for a PHP file, then passes the content material to a PHP interpreter which executes the code. The server returns ensuing output (sometimes HTML or JSON) to the calling browser. In different phrases, the code runs dynamically on each request.

Node.js takes a unique strategy for net apps: your JavaScript utility is an internet server. Operating node index.js initializes the app, masses all modules, and launches a server which may reply to incoming requests. Altering a file makes no distinction to the app’s output as a result of it’s already working in reminiscence. To check updates, you need to shut it down with Ctrl | Cmd + C and run node index.js once more.

Node’s cease and restart course of turns into irritating while you’re making a number of adjustments throughout debugging or these uncommon instances of undisturbed productiveness. Happily, there are two options:

  1. nodemon
  2. Node.js –watch mode


nodemon is a third-party Node.js module developed by JavaScript guru Remy Sharp. (He says you’ll be able to pronounce it as you select!)

You possibly can set up nodemon as a worldwide module:

npm set up -g nodemon

Then change node with nodemon in growth start-up instructions. For instance, think about this command:

node --inspect index.js arg1 arg2

The command above will now appear like this:

nodemon --inspect index.js arg1 arg2

Your utility begins as regular, however it’ll robotically restart while you edit and save a supply file. There’s no must press Ctrl | Cmd + C and run once more, though you’ll be able to sort rs and press Enter to drive a restart.

Observe: nodemon is a server-side resolution and doesn’t refresh any browser tabs you may have pointed at your app. You possibly can implement reside reloading with instruments corresponding to Browsersync or esbuild.

For nodemon assist, enter:

nodemon --help

nodemon Configuration

nodemon has its personal set of command-line arguments which take precedence over configuration elsewhere. You may also outline configuration in:

  • a "nodemonConfig" part in your venture’s bundle.json file
  • a neighborhood nodemon.json configuration file within the venture listing, and/or
  • a worldwide nodemon.json configuration file used when working nodemon --config <file> from the command line

The next parameters/settings can are generally used.


nodemon displays JavaScript information within the present working listing, however you’ll be able to explicitly set particular paths with wildcards on the command line:

nodemon --watch lib1 config/*.json ./index.js

Or you are able to do this in a nodemon.json configuration file:

  "watch": [


Equally, you’ll be able to select to disregard paths:

nodemon --ignore lib2 config/construct.json ./index.js

Or you are able to do this in a nodemon.json configuration file:

  "ignore": [


You possibly can monitor particular information by their extension. For instance, you’ll be able to monitor js, cjs, mjs, json, and njk template information like so:

nodemon --ext "js,cjs,mjs,json,njk" ./index.js

Or you are able to do so in a nodemon.json configuration file:

  "ext": "js,cjs,mjs,json,njk"


File watching can fail in some environments, corresponding to Docker containers studying information from a mounted drive. Switching to legacy watch mode makes use of polling to verify whether or not information have modified. From the command line:

nodemon --legacy-watch ./index.js

Or in a nodemon.json configuration file:

  "legacyWatch": true


nodemon waits one second earlier than triggering a restart. This may be helpful while you’re sometimes saving many information directly. You possibly can change the delay from the command line — for instance, to 5 seconds:

nodemon --delay 5 ./index.js

Or in a nodemon.json configuration file (notice this makes use of milliseconds quite than seconds):

  "delay": 5000


Exhibits verbose output logs:

nodemon --verbose ./index.js

Or in a nodemon.json configuration file:

  "verbose": true


Units particular atmosphere variables a nodemon.json configuration file:

  "env": {
    "NODE_ENV": "growth",
    "SERVER_PORT": 8000

Different executables

Lastly, you’ll be able to launch purposes written in different languages utilizing nodemon. For instance, to start out a perl script which restarts robotically:

nodemon --exec "perl" ./app.pl

You may also outline lists of executables in a nodemon.json configuration file with their extension title:

  "execMap": {
    "pl": "perl"

Superior nodemon

nodemon offers extra superior functionalit,y must you require it:

Node.js --watch Mode

nodemon stays the software of selection when you have subtle utility start-up necessities. Nonetheless, if you happen to’re utilizing Node.js 18.11 (launched late 2022) or newer, it offers an experimental --watch choice to restart your app with out having to put in a nodemon or every other third social gathering module. For instance, to the beginning command:

node --inspect index.js

This turns into:

node --inspect --watch index.js

Node.js restarts when any imported file adjustments. There are not any different management choices, so if it’s not appropriate on your venture, think about using nodemon as a substitute.


Auto restarting your Node.js app is one thing you’ll discover more and more helpful as your expertise will increase. Contemplate it as a part of your growth workflow in all of your tasks.

Supply hyperlink

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles