Table of Contents

Automatic reload node.js files

We'll use nodemon for this.

Nodemon is a tool meant for development. It should not be used in a production environment.

Install

To automatic load the files, without a restart being needed, we should install:

npm install --save-dev nodemon

The –save-dev flag saves nodemon as a development dependency, meaning it won't be installed in production environments.

Usage

Use nodemon instead of node

Once installed, you can start your application with nodemon instead of node. For example, if you have a file server.js, you would start it with nodemon as follows:

npx nodemon server.js

This starts the server.js application and monitors for any changes.
If nodemon detects any changes in your source files, it will automatically restart your server.

Npm scripts

You can also add nodemon as a start script in your package.json. This way, you can start your application with nodemon by running npm start.
Here is an example of how you would do this:

"scripts": {
    "start": "nodemon server.js"
}

To run it:

npm start