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:
- Monitors file changes in the
application directory.
- Automatically restarts the
Node.js server on file updates.
- Lightweight and easy to
configure.
- Supports custom configurations
and scripts.
Installing nodemon
- Globally:
- Installs nodemon globally, making it
available for all projects.
o npm install -g nodemon
- Use the nodemon command from anywhere.
- 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
- Basic Usage:
- Replace node with nodemon to start your application.
o nodemon app.js
- 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
- 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
- Set a Specific File to
Watch:
- Use the -w or --watch flag:
o nodemon -w src app.js
- Run with a Custom Extension:
- Watch files with specific
extensions (e.g., .ts for TypeScript):
o nodemon --ext js,ts app.js
- Execute Custom Commands:
- Run a different command
instead of restarting the Node.js process:
o nodemon --exec "npm run build"
- 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:
- Increases development speed
by automating restarts.
- Provides flexibility with
configurations and commands.
- Simple to integrate with any
Node.js project.
Common Commands with nodemon:
- Start the server:
2. nodemon app.js
- Watch specific files:
4. nodemon -w src app.js
- Ignore files or directories:
6. nodemon --ignore node_modules app.js
- Run with a specific
extension:
8. nodemon --ext js,ts app.js
- Use a custom delay:
10.nodemon --delay 2.5 app.js
Best Practices with nodemon:
- Install nodemon locally for better
project-specific control.
- Use nodemon with npm scripts for standardization in
collaborative projects.
- Exclude unnecessary files or
directories to optimize performance.
- Use configuration files for
complex setups.
Let me
know if you'd like a practical example or further assistance!
Comments
Post a Comment