Restart a Node.js app automatically

Restart a Node.js app automatically

Run a node application in development mode that restarts on file changes.

By Mario Kandut

More articles:


When developing Node.js applications you have to restart the application each time changes are made to a file, otherwise your changes will not be applied and not be visible in the application. You can manually do this, by stopping the running node process and running it again. Though, this task can be automated with the help of nodemon. The tool nodemon helps develop node.js based applications by automatically restarting the node application, when file changes in the directory are detected.

Restarting a Node.js project on file changes

A typical Node.js applications has many npm scripts, of a few are start (to start the application) and dev (to start the development version). When using nodemon, it is recommended to create a npm script specifically for running the application during development, can be called dev or development or whatever you like, though dev is mostly used. Creating scripts for specific environments is best practice for separating commands and avoid bugs.

💰 Start your cloud journey with $100 in free credits with DigitalOcean.

Nodemon can be installed globally, or it can be installed locally as a devDependency.

Create a start script

First steps it to create a start script, which will run the application. Add an entry in package.json under the "scripts" field. It should look like this (assuming index.js would start the application):

"scripts": {
	"start": "node index.js"

Install nodemon

If you have installed nodemon globally, you can skip creating a dev script and just run the nodemon from the command line. Though everyone working on the project should have all the tools to get started. Globally-installed packages aren't distributed with your project, this means that if someone does not have nodemon installed globally they would not be able to use it within this project.

To install nodemon as a devDependency run:

npm i -D nodemon
# OR
npm install --save-dev nodemon

Create a dev script

After successfully installing nodemon, we can create a dev script, which will reload our app on file changes during development. The package nodemon has an --exec flag to run an npm script whenever a file has changed.

To create a dev script, add a new "dev" entry in the scripts field in package.json.

"scripts": {
	"start": "node index.js",
	"dev": "nodemon --exec 'npm start'"

In the dev script above, we tell nodemon to run the npm start script when a file has changed. One of the benefits of npm scripts is that they are able to run devDependencies without having to use the full path to the executable in the node_modules directory. By default, nodemon will try to restart index.js.

Run the dev script

Finally, run the dev script by typing npm run dev and enjoy nodemon restarting your application on file changes. To manually restart your Node.js application you can type rs in the command line.

Important: If your application writes to files, like updating a config file, nodemon will prematurely restart your application. To prevent this behaviour, nodemon has a flag for ignoring files or folders, and you can also use wildcards, for example: --ignore './folder/*.json'.


  • Creating npm scripts with unique purposes is useful to organize the tasks.
  • The package nodemon can automatically restart Node.js application in development mode.
  • Nodemon should be installed as a devDependency to provide the same tools to every person working on the project.
  • Npm scripts can be executed by nodemon with the --exec flag.
  • Files and folders can be ignored with the --ignore flag in nodemon, which prevents premature restarts of the application.

Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

nodemon, HeyNode

More node articles:

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑

We use cookies 🍪 to build a better website.! Close to accept or learn more.