Nodemon: Only Re-Run the Code When a Specific File Changes

Updated: December 30, 2023 By: Guest Contributor Post a comment

Nodemon is a utility that monitors for any changes in your Node.js applications and automatically restarts the server. This tutorial teaches how to configure Nodemon to only re-run code when specific files change, optimizing development workflows.

Before getting started, ensure you have Node.js installed. Knowledge of the terminal and fundamental Node.js concepts is also recommended.

Using the –watch flag

Installing Nodemon:

npm install -g nodemon

Basic Nodemon usage:

nodemon app.js

Where app.js is your entry file.

Monitoring specific files:

nodemon --watch app.js

Nodemon will now only restart when app.js changes.

For debugging with Nodemon, use the --inspect flag:

nodemon --inspect app.js

Modifying nodemon.json

Creating a nodemon.json file:

  "watch": ["server.js"]

Using wildcards to watch all JavaScript files in a specific directory:

  "watch": ["src/**/*.js"]

Ignoring Files:

  "ignore": ["src/tests/*"]

Type rs and press enter in the terminal where nodemon is running to restart Nodemon manually.

Scripting with package.json

  "scripts": {
    "dev": "nodemon -e js,ts --watch src/app.ts"

Now you can start your nodemon monitoring with npm run dev.


By specifying files, you can enhance your development workflow with Nodemon, ensuring only necessary code reloads. Tailor this versatile tool to your project’s needs for a more efficient coding experience.