My Development Notes


What is nodemon? How to use nodemon with NodeJS?


Nodemon is a very useful tool available on the NPM registry which helps us write node applications by automatically restarting the apps when any of the NodeJS file’s source code is changed i.e., whenever we edit or rewrite the source code as a developer.

How it’s done, you ask? Just by replacing the node terminal command with the nodemon command (globally) or by using npm run dev command (locally). Here’s how :

Installation

To install nodemon use the below terminal command and hit enter :

npm install nodemon

You can check this under the ‘dependencies’ in package.json file. Next, you go to your package.json file and add the following code under the scripts like so :

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon app.js"
  }

We’ve added “dev”: “nodemon app.js” to our main JS file which in our case is app.js, then open up the terminal and run npm run dev to activate and run nodemon. However, if you install nodemon globally, you can skip all the above steps and do as instructed in the below steps.

To install nodemon globally to be used anywhere you want, type the following terminal command :

npm install -g nodemon

After you install nodemon globally you can execute the JS file inside the terminal using the following terminal command :

nodemon<space>file.js

Uninstall Nodemon

To uninstall nodemon simply use the following command :

npm uninstall nodemon

Tip : It’s not recommended to install nodemon globally. It’s always better to use it locally to keep things simple. To learn more click here.

Using Nodemon as a development dependency

To use nodemon as a development dependency, i.e., to use it as a tool and not inside your code to test run things easily you develop, use the below code :

npm install --save-dev nodemon

Leave a Reply

Your email address will not be published. Required fields are marked *