This guide would show you how you can make your Node.js application reload automatically whenever you make changes to your code during development. It helps you to save a lot of effort from having to constantly switch between your code editor and terminal. The tool that is commonly used for this comes from the package nodemon.
How to install
There are two ways that you can use install Nodemon. You can install it as a global tool or as a development dependency. I would recommend to install it as a development dependency. However, I would still show you how you can install it globally as it is still a commonly used method.
Globally
The recommended way to install nodemon is via NPM. You can install it globally using:
$ npm install -g nodemon
With nodemon installed, you can just run your node application as usual except just changing
node
to nodemon
, such as:
$ nodemon index.js
Replace it with your entrypoint file name such as index.js
, main.js
, or
bin/www
depending on your setup. This is the easiest way to do it. Therefore, it is
the most common. Now, you can start your app and have it reload automatically when you are making
code changes.
Development dependency (recommended)
The second method, instead of installing it globally, is to install it as a development
dependency, also using NPM. This is the method I recommend mostly for collaboration reasons.
It ensures that Nodemon is installed as a development dependency whenever someone runs
npm install
. Moreover, it ensure consistent version and doesn't assume that
everybody has Nodemon installed. It avoids the issue of having missing dependencies which you may
know is an annoying issue to debug. Therefore, it is best you help others avoid that issue.
Install it and save it to your package.json
as a development dependency by running:
$ npm i --save-dev nodemon
Then, you can start either using npx
(since it's not a global dependency) or using
the nodemon entrypoint file from node modules ./node_modules/nodemon/bin/nodemon.js
.
$ npx nodemon index.js
# or
$ ./node_modules/nodemon/bin/nodemon.js index.js
The next step is to add a dev
script so that you don't have to run this eyesore
of a command. In your package.json
file, add the dev command alongside your start
command.
{
"scripts": {
"start": "node ./index.js",
"dev": "./node_modules/nodemon/bin/nodemon.js index.js"
}
}
Now you can start it with Nodemon using the conventional Node.js project development environment command.
$ npm run dev
Conclusion
There you have it. Now, you can effective hot-reload to quickly view your Node.js code changes.
You're using the commonly used Nodemon tool which you can install either globally to use on your
machine or locally as a development dependency in your project node_modules
. Nodemon
can also be used to do reload for code in other languages too. However, I highly recommend to use
tools that are conventional in those other languages instead.
I would also like to note in case it wasn't obvious, Nodemon is not a production tool! If you need a Daemon to keep your Node.js process alive when running in production, look in to something that is more specially made for that such as PM2.