NPM is an online repository of Node.js packages. It is a command line utility that comes installed with Node.js, and helps you manage all the client-side packages that you need in your web application. For example, if I need to use Angular, I can ask NPM to install it. NPM will keep track of that package in a special file called package.json and install it when needed.
Installing a package using NPM
NPM is Node.js Package Manager. Therefore, as a first step, you need to make sure that you have Node.js installed. Go to https://nodejs.org/en/download/ and download Node.js and install it. Follow the wizard and keep the defaults in all options. Once done, you have NPM available. You can make sure by typing the command npm -v on the command prompt. The -v option will tell you what version of NPM you are using.
With that step out of the way, I decided to have Angular installed. I am going to start with an empty application, but you can use the development environment you are most comfortable with. So I am going to install it using NPM next:
Issuing that command, however, results in the following error:
Figura 1. Error using NPM install without a package.json file
So what is the problem? It turns out that NPM keeps track of the libraries installed in a file called package.json. Since our application is empty (and this file does not exist), NPM is complaining. One approach is to go ahead and add the file. Put the content you want in it and you are ready to install angular. However, there is an easier way. I can run the following command, answer few questions regarding my application (or keep the defaults), and the file will be created for me. I will follow this approach:
Notice how I am entering some information and leaving others as default:
The system will prompt me if I am ready with the information entered. I am, so I type “y” and my package.json file is created. Investigate the content of the file and pay special attention to two entries: dependencies and devDependencies:
Figura 2. dependencies and devDependencies sections in the package.json file
Deciding between dependencies and devDependencies
When installing libraries, you have two options that help you put the dependencies under the appropriate entry. The difference between the entries is that if you need a library only during development and local testing purposes, you will put it under the devDependencies by using the --save-dev option. Otherwise, you will put it under the dependencies folder by using the –save option. The commands are:
npm install [library_name] --save
npm install [library_name] --save-dev
Go ahead and try these commands and see where the libraries are installed.
Updating and removing packages
Now, what happens if you decided to remove or update an existing package? You can start by looking for outdated packages. Here is how:
This will list for you the package(s) with current, wanted, and latest version. You can then run
to update to the latest version.
How about uninstalling a package? Let’s say you have lodash and now you want to remove it. Remember that the package is in the node_modules directory and the reference is in the package.json file. To remove the package completely, you issue the following command:
npm uninstall lodash --save
In this article, we discussed how to use NPM to install packages in our web applications. In the upcoming articles, we will discuss more commands in NPM and see how we can simplify our development tasks.