Automating Server Restarts with Nodemon
When developing applications, it can be tedious to manually restart the server every time you make a change. That’s where Nodemon comes in. It automatically restarts application when file changes are detected.
Setting Up Nodemon
First, install Nodemon as a development dependency:
npm install --save-dev nodemon
This adds Nodemon to the devDependencies section of your package.json file. These are tools needed only during development, not in production.
Using Nodemon
To start application with Nodemon, we can add a script to package.json:
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}
Now, you can start your server in development mode with:
npm run dev
And that’s it! With Nodemon, your server restarts automatically whenever you make changes to your code.
Note that you still need to manually refresh your browser as hot reloading isn’t available outside of environments like React.