In order to run Node.js applications one would have to type node along with the filename.
This process has to be repeated every time you want to run code. But let’s say you have a program that automatically runs your code whenever you make updates to the application? Wouldn’t that be great? It would save you time from the repetitive process of executing a command.
Fortunately such a program exists and it is called “Nodemon”. Nodemon monitors changes in your code and then automatically restarts your server whenever there is a change.
How to Install Nodemon?
Just Enter this command on the terminal
npm install -g nodemon
In my previous blogs, I talked about how to setup a node.js environment on a Mac and describing what npm is. Npm is used to Install packages or dependencies in a project folder. There is a file called package.json that lists the packages Installed.
This is how package.json looks like-
As you can see the dependencies in this file are nodemon and typescript. In a Node.js project you would something called as node_modules. The node_modules Include the libraries and packages Installed for the project.
Here is an example of a file structure for a Node.js Project
Incase one accidentally the node_modules folder, how can we retrieve or Install the dependencies?
This is where package.json is of help to us. Since package.json stores the list of dependencies that you Installed in the project, all you have to do is run
Once you run this on the command line, the node package manager would scan the package.json file and Install the dependencies from there. And from that you would get the node_modules folder back.
npm also known as the node package manager is used to Install dependencies for a project. Dependencies are files that are needed in order for a project to compile and build successfully. Without Installing dependencies, the project would always run Into errors.
Node Package Manager is a one stop shop for Installing dependencies. Without a package manager life would be quite chaotic. One would have to go from one website to another in order to Install files since they are scattered all across the web. It’s akin to shopping in mall where you can get everything in one place rather than going to different stores across the street. It’s a matter of perception which you find more easier or convenient.
As discussed, node package manager allows you to Install all the required files from one place. All you would have to do is just type this command in a terminal or a command prompt.
A package manager for Mac that you could use is Home Brew. In order to Install the Package Manager enter the following command in the terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Home
brew install node
This Installs Node.js and NPM. In order to ensure that node.js has been Installed, type this command in the terminal
If it returns a version number, it means that node.js has been successfully Installed.
Let’s summarise, there are two steps in Installing Node.js
- Install a Package Manager for Mac. In this case we have Installed HomeBrew.
- Once Installed, Install node by typing the install node command in the terminal.