Automatically Restart Node Apps on File Change with nodemon

# Automatically Restart Node Apps on File Change with nodemon

### Introduction

Nodemon is an open-source utility package that keeps track of the changes made to your source code and restarts your project server automatically when new modifications are made.

It also offers some unique features, such as the ability to watch a directory rather than simply files, as well as to ignore particular files that may be present in a given directory.

In this guide, we'll take a look at how to integrate Nodemon into your Node projects, to alleviate the hassle of restarting your projects every time you make a change, and increase the speed of development.

### Why Nodemon?

Traditionally, when we run Node applications, we use the node command along with the file name:

$node app.js  This, well, runs the Node application by loading in the latest files and their newest states, and using them as they are. This state resides and persists in memory while the application is running, so even if you modify the source files - this isn't reflected in the currently running application. Once you stop the application, the original contents of the files are released from memory, and the state is lost until you load it in again by running the application. If you've changed the files - the new state is used and the application is updated when run again. During development - we typically start and stop applications many times to "test out" whether something we wrote works as expected. These are oftentimes small changes, such as changing the color of a button, or adding an element in a given position. Quickly, it gets annoying to have to restart the entire project for small changes during development. Nodemon alleviates this annoyance, by automatically restarting and updating your project whenever you make a change in the source files, without you having to explicitly restart the project. ### Installing Nodemon The fastest way to get started with Nodemon is to install it as a global npm package: $ npm install -g nodemon


You can also install Nodemon as a development dependency for just a single project:

$npm install --save-dev nodemon  ### Getting Started with Nodemon Once the package is installed - it can be used instead of the node command when running a project. Instead, you can use the wrapper nodemon command, which surely enough, runs the application just as node does, but it also keeps track of the files in the directory and triggers a restart whenever the files are changed. Note: If you install the package globally, the command will be available globally. If you've installed it for a project, it'll only be available within your project's directory. Let’s say we have a file, app.js, in which we generate a random number and display it on the console: let randNo = Math.floor(Math.random() * 10); console.log(randNo);  We can then run this file with Nodemon: $ nodemon app.js
[nodemon] 2.0.15
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting node app.js
6
[nodemon] clean exit - waiting for changes before restart


Note: If you encounter an error: nodemon: command not found - run it via npx.

$npx nodemon app.js [nodemon] 2.0.15 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting node app.js 8 [nodemon] clean exit - waiting for changes before restart  Now, if you were to change anything in the file, and save the change to commit it to the file system, such as adding a whitespace after the last line - as soon as you save the file, the project will be restarted, and another, new number will be displayed: $ npx nodemon app.js
[nodemon] 2.0.15
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting node app.js
8
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting node app.js
6
[nodemon] clean exit - waiting for changes before restart


Here, we're given some extra information! Nodemon is watching files with certain extensions - js, mjs and json. Additionally, it's watching a certain path. Let's take a look at how to customize which path Nodemon watches.

#### Watch Directories

The nodemon command also supports a --watch flag for watching different directories. Assuming we have a directory, app, that we want Nodemon to monitor, we simply supply the directory after the --watch flag:

$nodemon --watch app  And if, for example, we have multiple directories like in the following structure: app views utils public/ index.html  We can add the  --watch flag to each directory to include them: $ nodemon --watch app --watch views --watch utils --watch public


#### Ignore Files and Directory

It's also possible to tell Nodemon to avoid watching certain files and folders, which is useful if you don't want Nodemon to restart your files prematurely or too often, especially if a file is changed programmatically all the time, such as a log file.

We can do this with the --ignore flag:

$nodemon --ignore views/ --ignore public/index.js  #### Delay Restarting While delaying the restart isn't too common, there are times when the files you want to watch are not instantly available, or a batch of files is being updated successively. For each change, a restart would be issued, making your application restart multiple times. ## Free eBook: Git Essentials Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! You can delay when Nodemon reacts to a changed file by setting the --delay flag, followed by a delay in seconds: $ nodemon --delay 5 app.js


Here, we've added a 5-second delay (wait time) before Nodemon reacts to a change. Any changes made in between these 5 seconds will restart the delay timer, and ignore the changes made before, only executing a restart for the latest change.

It's also possible to specify the delay in milliseconds:

// Set delay for 10 seconds 50 milliseconds.
$nodemon --delay 10.5 app.js // Set delay for 2000 miliseconds.$ nodemon --delay 2000ms app.js


### Conclusion

Nodemon allows you to automate the process of restarting Node applications when you make changes to the underlying files. This isn't an issue per se, but can become an annoyance when restarting an application many times during development.

It watches the source directory for changes, and restarts your Node applications seamlessly and allows for customization in the process.

Last Updated: February 9th, 2022