Introduction
During development, restarting your Node.js server manually after every code change can be tedious. nodemon is a popular development tool that automatically monitors your project files and restarts the server whenever changes are detected, making the development workflow faster and more efficient.
Information
What is nodemon?
nodemon is a command-line utility that wraps your Node.js application. It watches for file changes and automatically restarts the application whenever source files are modified.
Installing nodemon
Install as a Development Dependency
Installing nodemon locally keeps it available only for your project and ensures everyone working on the project uses the same version.
Install nodemon locally
npm install --save-dev nodemonInstall Globally (Optional)
A global installation allows you to use the nodemon command from any project on your system.
Install nodemon globally
npm install -g nodemonTip
Project Structure
Creating a Simple Server
server.js
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/plain"
});
res.end("Hello from Node.js!");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});Running the Application with nodemon
Instead of using the node command, start your application with nodemon.
Start the server
npx nodemon server.jsIf nodemon is installed globally, you can run:
Run using global installation
nodemon server.jsWhat Happens Behind the Scenes?
Adding a Development Script
Instead of typing the full command every time, define a script inside package.json.
package.json
{
"scripts": {
"dev": "nodemon server.js"
}
}Start the development server using:
Run development script
npm run devWatching Additional File Types
By default, nodemon watches JavaScript files. You can configure it to monitor additional file types such as JSON, TypeScript, or template files.
Watch multiple extensions
nodemon --ext js,json,ts server.jsUsing a nodemon Configuration File
Create a nodemon.json file to avoid repeatedly specifying command-line options.
nodemon.json
{
"watch": ["src"],
"ext": "js,json",
"ignore": ["node_modules"],
"delay": "1000"
}Configuration Options
| Option | Description |
|---|---|
| watch | Directories or files to monitor. |
| ignore | Files or folders to exclude from monitoring. |
| ext | File extensions to watch. |
| delay | Wait time before restarting after a change. |
| exec | Custom command used to execute the application. |
Typical Development Workflow
Advantages of Using nodemon
- ⥠Eliminates manual server restarts.
- đ Speeds up development.
- đ ī¸ Easy to install and configure.
- đ Watches multiple file types.
- âī¸ Supports configuration through nodemon.json.
- đģ Works well with most Node.js projects.
Common Commands
| Command | Purpose |
|---|---|
| npx nodemon server.js | Run using the local installation. |
| nodemon server.js | Run using the global installation. |
| npm run dev | Start using the package script. |
| rs | Manually restart the server while nodemon is running. |
| Ctrl + C | Stop the running process. |
Common Issues
nodemon Command Not Found
This usually occurs when nodemon is not installed globally or is not available in the system path. Using npx nodemon avoids this issue for locally installed versions.
Server Does Not Restart
- Ensure the modified file is inside a watched directory.
- Verify the file extension is included in the watch list.
- Check that the file has been saved.
Frequent Restarts
If your application restarts too often, ignore generated files and build directories such as dist or node_modules using the ignore option.
Best Practice
Comparison: node vs nodemon
| Feature | node | nodemon |
|---|---|---|
| Runs JavaScript files | â | â |
| Automatic restart | â | â |
| File watching | â | â |
| Development focused | â | â |
| Production runtime | â | â |
Official Resources
nodemon GitHub Repository and nodemon Package on npm