Add nodemon to an Express App to Restart Node Automatically
Set up nodemon to streamline our Node.js development workflow
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.
This lesson preview is part of the Build a Spotify Connected App 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 Build a Spotify Connected App, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:05] Alright, so from the last module, we should have a basic express app set up. Let's take a look at where we are.
[00:06 - 00:11] So here's our package.json. We have express installed as a dependency.
[00:12 - 00:17] And here's our index.js file. Our index.js file has a few different things in it.
[00:18 - 00:26] It has an index route and an awesome generator route. And we also tell our app to listen on port 8888.
[00:27 - 00:40] And also make sure you have a package.lock.json file and a node modules folder. If you don't have those already, make sure to cd into your directory and run npm install.
[00:41 - 01:01] If you remember from the last module, if you wanted to update any of the code in our index.js file, we would have to run node index.js in our terminal and then visit OCOhost8. And every time we made a change, we would have to kill this process and run it again.
[01:02 - 01:07] But that's pretty annoying. And there's a tool we can use to speed up that process.
[01:08 - 01:19] So that tool is called NodeMon. And NodeMon, like they say, is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
[01:20 - 01:30] And that's exactly what we want. If we update any line in index.js and hit save, we want our localhost 8888 to be updated.
[01:31 - 01:45] So let's go ahead and add NodeMon to our project. In our terminal, we'll npm install - - save dev nodemon.
[01:46 - 01:58] Once that's installed, we can head over to our package.json and add an npm script in order to run NodeMon when we run npm start. So we can get rid of this test script.
[01:59 - 02:08] And instead say start, nodemon, index.js. And all we have to do is pass the nodemon command, the name of the file we want to watch.
[02:09 - 02:24] Give it a save. And now, if we head over to our terminal again and run npm start, you should see that nodemon is started and our express app is listening at localhost 8888.
[02:25 - 02:45] To make sure it's working, we can head on over to our index.js file and maybe update the JSON that's being returned in our index route. And if we give that a refresh, we should see that name is now hello and it updates accordingly.
[02:46 - 02:52] Awesome. So, now that our development environment is running smoothly, we'll learn a bit about OAuth in the next lesson.