ExplainerProgrammingTech/Web

Nodemon – Automatic Restart the node application

Introduction

Nodemon is a widely used tool in the Node.js ecosystem that helps developers by automatically restarting applications when file changes are detected. It streamlines the development workflow by eliminating the need for manual restarts, making development faster and more efficient. This article provides an in-depth look at Nodemon, including its features, installation, usage, and advanced configurations.

What is Nodemon?

Nodemon is a utility that monitors changes in files and automatically restarts a Node.js application when modifications are detected. It is particularly useful in backend development, where frequent code changes require application restarts to reflect updates.

Key Features

  1. Automatic Restarting: Detects file changes and restarts the application automatically.
  2. Zero Configuration: Works out of the box without needing extensive setup.
  3. Custom Watch Directories: Allows monitoring of specific directories and files.
  4. Ignore Rules: Prevents unnecessary restarts by ignoring specified files or folders.
  5. Support for Multiple File Extensions: Watches JavaScript, JSON, and other relevant file types.
  6. Compatibility with Various Environments: Works on Windows, macOS, and Linux.

Installation

Nodemon can be installed globally or locally, depending on the project requirements.

Global Installation

To install Nodemon globally on your system, use:

npm install -g nodemon

This allows you to use Nodemon in any Node.js project without requiring additional installation.

Local Installation (Project-Specific)

For project-specific usage, install Nodemon as a development dependency:

npm install --save-dev nodemon

This ensures Nodemon is only available within the project and not system-wide.

Basic Usage

Nodemon can be used to start Node.js applications with minimal configuration.

Running a Script

To start a Node.js script using Nodemon, use:

nodemon app.js

Here, app.js is the entry point of your application. Nodemon will monitor it and restart the application when changes occur.

Running with NPM Scripts

Instead of manually running Nodemon, it can be configured in the package.json file under scripts:

"scripts": {
  "start": "node app.js",
  "dev": "nodemon app.js"
}

Now, the application can be started using:

npm run dev

Configuration and Customization

Nodemon provides several configuration options to enhance its functionality.

Specifying Extensions to Watch

By default, Nodemon watches .js, .json, and .mjs files. To watch additional file types, use:

nodemon --ext js,json,html

Ignoring Files and Directories

To prevent Nodemon from monitoring certain files or directories, create a .nodemonignore file:

node_modules/
logs/
*.log

This prevents unnecessary restarts due to changes in ignored files.

Custom Restart Delay

To add a delay before restarting, use:

nodemon --delay 2.5

This adds a 2.5-second delay before restarting the application.

Using a Configuration File

Instead of passing options via the command line, you can create a nodemon.json configuration file:

{
  "watch": ["src"],
  "ext": "js,json",
  "ignore": ["logs/*"],
  "delay": "2"
}

This file allows for easier management of Nodemon settings.

Advanced Usage

Running Scripts with Nodemon

Nodemon can execute custom scripts before or after restarting the application:

nodemon --exec "npm test"

This runs the test script automatically when changes occur.

Using Nodemon with Other Languages

Although designed for Node.js, Nodemon can monitor and restart applications written in other languages:

nodemon --exec python app.py

This will restart the Python script app.py when file changes are detected.

Debugging with Nodemon

Nodemon can be used in conjunction with debugging tools:

nodemon --inspect app.js

This enables debugging mode with automatic restarts.

Benefits of Using Nodemon

  • Increased Developer Productivity: Eliminates manual restarts, allowing for a more efficient development process.
  • Better Workflow Automation: Works seamlessly with package managers and build tools.
  • Reduced Errors: Prevents issues caused by forgetting to restart the server after code changes.

Conclusion

Nodemon is an essential tool for Node.js developers, simplifying the development process by automatically restarting applications when code changes occur. Its flexibility, ease of use, and powerful customization options make it a must-have for backend development. Whether you’re working on small projects or large-scale applications, Nodemon enhances productivity and streamlines the development workflow.

Useful Links


ExpressJS – Web framework for Node.js

 

How To Install Node.js on Ubuntu

 

 

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

2 thoughts on “Nodemon – Automatic Restart the node application

Leave a Reply

Your email address will not be published. Required fields are marked *