Developing applications requires frequent changes to the codebase and this means that you will have to keep restarting the application each time the files are modified so that you can see the changes reflect in the application. This can be very tedious and time-consuming. It is a developer's job to ensure that redundant steps such as this one are omitted from the process and that is where Nodemon comes in.
Nodemon is a powerful command-line tool that automates the process of restarting the application every time changes are detected in a file. It is specifically designed for NodeJS applications making the development process more efficient.
In this article, you will learn how to set up Nodemon in your projects and use it to track changes in your files. Let's get started.
A few things are a must-have before we can implement Nodemon into your projects.
- Node installed locally on your machine - Check out our tutorial on how to install node using NVM
- Make sure
npmis installed. This installs automatically once Node is installed. Confirm by running this command.If it's installed, you should see the node version.
- Basic knowledge of how to work with NodeJS - This project explain the basic knowledge of working with Node.
Nodemon installation - Global
With that out of the way, we can install Nodemon globally or as a devDependency. The difference between global installation and installation as a dependency is that global installations only work on your machine. The packages are not distributed with your project. This means that if any other person working on the same application does not have Nodemon installed on their machine globally, they can't use it to automatically track changes to files. Dependent installations on the other hand are linked to the project hence everyone working on the project has the same tools.
To install it globally, run the command below.
npm install -g nodemon
Let the installation complete and Nodemon will be available as a command-line tool. You will be able to access it in any project without the need to create a dev- script.
Nodemon Installation as devDependency
To install Nodemon as a devDependency, run the following command inside your projects' folder.
npm install --save-dev nodemon
The command above will install Nodemon in your project. If you open your Package.JSON file, you should notice Nodemon under the devDependencies.
For the tool to work with your project, one must create a dev script that will be triggered every time a file changes. To do this, open your
Under the Scripts entry, add a new script with the name
devand make its value
nodemon --exec 'npm start'. This is how your scripts entry should look like assuming you have not added any other scripts.
"start": "node app.js",
"dev": "nodemon --exec 'npm start'"
The code above shows that my entry point is app.js. This is set when initializing a project with the
npm init command. Don't mind if your is different. We also see that Nodemon is set to run a script rather than running a file. Anytime we run Nodemon, the start script is triggered which in turn triggers the entry point of our project (app.js).
Now that Nodemon is installed and the scripts are in place, we can start Nodemon by running the
dev script. To do so, run the command below in your terminal. The terminal should be pointing to the root directory of your project for it to work. Unless you installed Nodemon globally.
npm run dev
The command above should start your application and stay on standby to track changes to your files. If you wish to restart Nodemon manually while it is running, type
rs into the terminal and press
Nodemon is a very useful tool for NodeJS developers. It provides an efficient way to automatically restart your application any time there is a file change. This streamlines the development workflow by allowing developers to worry about code rather than restarting the application every single time they make a change to the codebase. This automation also saves time by doing away with redundant processes. That is it for this one.
If you found the article helpful, consider sharing it and subscribing to Geekbits.
Thank you for reading : )