Auto-Reload with nodemon
nodemon restarts your server automatically whenever you save a file.
Syntax
nodemon app.jsNormally you must stop and restart node app.js after every change. nodemon watches your files and restarts the server for you, which makes development much faster.
Setup
- Install it as a dev dependency:
npm install --save-dev nodemon. - Add a script to
package.json. - Run
npm run dev.
Example
{
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
}
}When to use it
- A developer installs nodemon as a dev dependency so the Express server restarts automatically every time they save a route file.
- A team configures nodemon to watch only the src/ directory, preventing unnecessary restarts when test or documentation files change.
- A nodemon.json config specifies a 500 ms delay before restarting so rapid consecutive saves do not trigger multiple restarts.
More examples
Install and run nodemon
Installs nodemon as a dev dependency and uses npx to launch it without a global install.
npm install --save-dev nodemon
npx nodemon index.jsDev start script in package.json
Adds a 'dev' npm script that starts the server under nodemon, keeping 'start' as the plain Node production entry.
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}nodemon.json configuration file
A nodemon.json config that watches only src/, restarts on .js or .json changes, ignores test files, and waits 500 ms before restarting.
{
"watch": ["src"],
"ext": "js,json",
"ignore": ["src/**/*.test.js"],
"delay": 500
}
Discussion