What is nodemon? | Lecture 8

 

Notes on nodemon Module in Node.js


What is nodemon?

  • nodemon is a utility that helps developers automatically restart a Node.js application whenever file changes are detected.
  • It’s a development tool that improves productivity by eliminating the need to manually restart the server after code modifications.

Key Features of nodemon:

  1. Monitors file changes in the application directory.
  2. Automatically restarts the Node.js server on file updates.
  3. Lightweight and easy to configure.
  4. Supports custom configurations and scripts.

Installing nodemon

  1. Globally:
    • Installs nodemon globally, making it available for all projects.

o   npm install -g nodemon

    • Use the nodemon command from anywhere.
  1. Locally (Recommended):
    • Installs nodemon as a development dependency.

o   npm install --save-dev nodemon

    • Use it in a specific project via npm scripts.

Using nodemon

  1. Basic Usage:
    • Replace node with nodemon to start your application.

o   nodemon app.js

  1. Running via package.json:
    • Add a script in package.json:

o   "scripts": {

o     "start": "node app.js",

o     "dev": "nodemon app.js"

o   }

    • Run the script:

o   npm run dev


How nodemon Works

  • nodemon watches for file changes in the current directory and subdirectories.
  • When a file changes, it stops the current process and restarts it with the updated code.

Customizing nodemon Behavior

  1. Ignore Specific Files or Directories:
    • Use a nodemon.json configuration file or the --ignore flag.
    • Example (nodemon.json):

o   {

o     "ignore": ["*.test.js", "node_modules/"]

o   }

    • Example (Command Line):

o   nodemon --ignore node_modules app.js

  1. Set a Specific File to Watch:
    • Use the -w or --watch flag:

o   nodemon -w src app.js

  1. Run with a Custom Extension:
    • Watch files with specific extensions (e.g., .ts for TypeScript):

o   nodemon --ext js,ts app.js

  1. Execute Custom Commands:
    • Run a different command instead of restarting the Node.js process:

o   nodemon --exec "npm run build"

  1. Delay Restarting:
    • Add a delay before restarting the application:

o   nodemon --delay 2.5 app.js


Creating a Configuration File

  • nodemon can be configured using a nodemon.json file in the project root.
  • Example:

·        {

·          "watch": ["src"],

·          "ext": "js,json",

·          "ignore": ["node_modules", "logs"],

·          "exec": "node app.js"

·        }


Advantages of nodemon:

  1. Increases development speed by automating restarts.
  2. Provides flexibility with configurations and commands.
  3. Simple to integrate with any Node.js project.

Common Commands with nodemon:

  1. Start the server:

2.  nodemon app.js

  1. Watch specific files:

4.  nodemon -w src app.js

  1. Ignore files or directories:

6.  nodemon --ignore node_modules app.js

  1. Run with a specific extension:

8.  nodemon --ext js,ts app.js

  1. Use a custom delay:

10.nodemon --delay 2.5 app.js


Best Practices with nodemon:

  1. Install nodemon locally for better project-specific control.
  2. Use nodemon with npm scripts for standardization in collaborative projects.
  3. Exclude unnecessary files or directories to optimize performance.
  4. Use configuration files for complex setups.

Let me know if you'd like a practical example or further assistance!

 

Comments

Popular posts from this blog

Introduction to Node.js | Lecture 1

Introduction to NPM | Lecture 2

Modules in Node.js | Lecture 4