How to Use Nodemon to Auto Reload Node.js
Nodemon is an open-source utility tool that helps automatically restart a Node server whenever a change in code is detected in development. In this lesson, we'll install Nodemon and introduce an application script that will start the Node server with Nodemon.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 00:11] When developing our server application, there are going to be a lot of changes we're going to make to our code. We probably don't want to stop our running server and restart it every single time you make an update.
[00:12 - 00:23] Wouldn't it be nice if there was actually a third party tool that sort of just didn't update for us or re-ran the server whenever we made a code change? Well, there is and it's called NodeMon.
[00:24 - 00:39] NodeMon is a tool that would monitor changes in our source code and automatically restart our server whenever a change is made. So to install NodeMon, we'll first exit the server we have running and then run the NPM install command.
[00:40 - 00:49] In this case, however, we're going to add in another argument that is known as the -d flag. -d is a shorthand version of --save-death.
[00:50 - 01:15] And essentially, what it basically states is that we want to install this particular package as a development dependency. After NodeMon is installed, in our package.json file, we will actually see that NodeMon is listed as a "debt dependency".
[01:16 - 01:36] Now we'll look to create a script in our application that can run the server with the NodeMon command. In Node applications, we're able to define NodeScript commands that are expected to be run in an app within a script section of the package.json file.
[01:37 - 01:48] Having script in our package.json file helps us avoid typing long commands manually on the terminal. What we'll do here is create a new script called start.
[01:49 - 02:03] This start script will be responsible in running our Node server. But in this case, we're going to use the NodeMon command to do so, since Node Mon is now available as a "debt dependency" within our application.
[02:04 - 02:19] Just like the Node command, we can just simply reference the location in which we want the Node server to actually run. In this particular case, we'll reference the source folder, which will contain the index.js file, which has the JavaScript code that is to be run.
[02:20 - 02:34] We'll save the package.json file, and now in our terminal, to simply run our server, all we have to do is run the npm run start command. By doing so, we can see that NodeMon actually outputs some messages to the console.
[02:35 - 02:50] It tells us the version of NodeMon being used, and some other messages such as the ability to restart any time, and the location in which way it's watching. At the very bottom, we can see that the console log message we've specified in our index file is also being outputted.
[02:51 - 03:06] That tells us that our server is now actually being run at localhost 9000. Now if we head back to the index.js file, and attempt to change anything in this file, such as let's assume I want to change this hello world message to another string like hello again.
[03:07 - 03:25] By just saving this file, we can see that NodeMon detects this change and actually tells us that it's restarting due to changes, and starting the same folder we specified before, the source folder. And then we can see the console log message be specified once again.
[03:26 - 03:39] This is really good, because now we don't have to actually manually exit and restart the server. NodeMon is a fantastic development dependency, since it watches for changes and simply restarts the server whenever there's changes being made.
[03:40 - 03:43] For now, we'll just simply reset this message back to hello world. So, that's it.
[03:44 - 03:45] Thank you. Thank you.
[03:46 - 03:47] Thank you. Thank you.
This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
![Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL](/_next/image?url=https%3A%2F%2Fs3.amazonaws.com%2Fassets.fullstack.io%2Fn%2F20221018174419041_2%2520%252816%2529.png&w=3840&q=75)