đ Introduction
During development, every change made to your Express.js application normally requires manually stopping and restarting the server. nodemon is a popular development utility that automatically restarts your Node.js application whenever it detects changes in your project files. This significantly improves the development workflow by providing instant feedback after every code modification.
Information
đ¤ Why Use nodemon?
- Automatically restarts the server after file changes.
- Eliminates the need to manually restart the application.
- Speeds up development and testing.
- Works with Express.js, Node.js, and other JavaScript applications.
- Simple to install and configure.
đ Prerequisites
- Node.js installed.
- An Express.js project initialized with package.json.
- Basic Express server created (for example, index.js).
đ Installation Methods
Installing nodemon locally makes it available only within the current project and is the recommended approach.
Install as a Development Dependency
npm install --save-dev nodemonInstalling globally allows you to use the nodemon command from any project on your computer.
Install Globally
npm install -g nodemonBest Practice
đ Updating package.json
Instead of running node index.js, create a development script inside the scripts section of package.json.
package.json
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}âļī¸ Running the Application
Start with nodemon
npm run devThe server starts normally, but now nodemon continuously watches your project files. Whenever you save changes, it automatically restarts the server.
đ Example Express Server
index.js
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Server is running with nodemon!');
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});đ How nodemon Works
đ Typical Development Workflow
đ node vs nodemon
| Feature | node | nodemon |
|---|---|---|
| Automatic Restart | â No | â Yes |
| Built into Node.js | â Yes | â No |
| Development Tool | Basic runtime | Optimized for development |
| Manual Restart Required | â Yes | â No |
âī¸ Optional Configuration
You can customize nodemon by creating a nodemon.json configuration file.
nodemon.json
{
"watch": ["src"],
"ext": "js,json",
"ignore": ["node_modules"],
"exec": "node index.js"
}This configuration tells nodemon which folders to watch, which file extensions to monitor, which directories to ignore, and which command to execute when restarting the application.
đĄ Best Practices
- Install nodemon as a development dependency using --save-dev.
- Use the npm run dev script during development.
- Use node or npm start for production deployments.
- Exclude unnecessary folders such as node_modules from file watching.
- Store custom settings in a nodemon.json file for consistency across the development team.
Warning
đ Learn More
Visit the official resources for additional information:
âĸ nodemon Official Website
âĸ nodemon GitHub Repository
âĸ Express.js Official Documentation