Learning Node.js Development
上QQ阅读APP看书,第一时间看更新

Installing the nodemon module

Now, to automatically restart our app as we make changes to a file, we have to install a command-line utility, and we'll do this using npm. To get started, we'll go to Google Chrome (or the browser you are using) and head over to https://www.npmjs.com, as we did previously in the Installing the lodash module in our app section, and the module we're looking for is called nodemon.

The nodemon will be responsible for watching our app for changes and restarting the app when those changes occur. Right here, as we see in the following screenshot, we can view the docs for nodemon as well as various other things such as current version numbers and so on:

You will also notice that it's a really popular module, with over 30,000 downloads a day. Now, this module is a little different from the one we used in the last section, that is, lodash. The lodash got installed and added into our project's package.json file as shown in the following code block:

{
"name": "notes-node",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.4"
}
}

That means it went into our node_modules folder and we were able to require it in our app.js file (refer to the previous section for more detail). Nodemon, however, works a little differently. It's a command-line utility that gets executed from the Terminal. It will be a completely new way of starting our application, and to install modules to be run from the command line, we have to tweak the install command that we used in the last section.

For now, we can start off much the same way, though. We'll use npm install and type the name just like we did in the Installing the lodash module in our app section, but instead of using the save flag, we'll use the g flag, which is short for global, as shown here:

npm install nodemon -g

This command installs nodemon as a global utility on your machine, which means it'll not get added to your specific project and you'll never require nodemon. Instead, you'll be running the nodemon command from Terminal, as shown here:

When we install nodemon using the preceding command, it'll go off to npm and fetch all of the code that comes with nodemon.

And it'll add it into the installation where Node and npm live on your machine, outside the project you're working on.

The npm install nodemon -g command could be executed from anywhere in your machine; it does not need to be executed from the project folder since it doesn't actually update the project at all. With this in place, though, we now have a brand new command on our machine, nodemon.