When you change the HTML, CSS, or JavaScript for a browser, you need to refresh the browser for the changes to take effect. The same thing applies to a server — you need to restart the server if you made any changes.
To restart the server, you need to:
- Stop the server with
Ctrl+C - Then start the server again with
node server.js.
This can be a frustrating process when you’re developing.
We can make the process easier with a tool called Nodemon. Nodemon detects changes in any JavaScript file and restarts the server for you automatically.
Installing and using Nodemon
You can install Nodemon with npm.
npm install nodemon -g-g here is a global flag. This means we’re installing Nodemon on your computer (and not inside the project). When we do this, we can use the nodemon command anywhere.
Using Nodemon is easy. You replace the node command with the nodemon command when you start a server.
nodemon server.jsA better way to install Nodemon
The better way is to install nodemon in the project.
If we do this, we allow other developers to have access to nodemon when they install the dependencies of the project. (This is an optional step if you’re developing by yourself).
To install Nodemon locally (in the project), you can use the -D flag.
npm install nodemon -DThis saves nodemon as a devDependency in the package.json file.
We do this because we only use Nodemon when we’re developing locally and not in a production server.
To use Nodemon that’s installed locally, you can use npx nodemon.
npx nodemon server.jsThat’s all you need to know about Nodemon.
Let’s move on.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson