Nodemon automatically restarts your Node.js server when you change your code. This saves time and effort during development.
0
0
Nodemon for development reloading in Express
Introduction
When you want your Express server to reload automatically after code changes.
When you are actively developing and testing your backend API.
When you want to avoid manually stopping and starting your server.
When you want faster feedback while fixing bugs or adding features.
Syntax
Express
nodemon [your_node_app.js]
Run this command in your terminal instead of node your_node_app.js.
You can configure nodemon with a nodemon.json file for advanced options.
Examples
Starts nodemon to watch and reload
index.js automatically.Express
nodemon index.js
Watches the
src folder for changes and reloads server.js.Express
nodemon --watch src server.js
Reloads when files with
.js or .json extensions change.Express
nodemon --ext js,json app.js
Sample Program
This simple Express server responds with 'Hello, Nodemon!' on the home page. Run it with nodemon index.js to see automatic reloads when you edit the file.
Express
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, Nodemon!'); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });
OutputSuccess
Important Notes
Install nodemon globally with npm install -g nodemon or as a dev dependency with npm install --save-dev nodemon.
Use nodemon only in development, not in production environments.
You can stop nodemon anytime by pressing Ctrl + C in the terminal.
Summary
Nodemon watches your Node.js files and restarts the server automatically.
It speeds up development by removing the need to restart manually.
Use simple commands like nodemon index.js to start.