× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Getting started with NPM

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 packages that you need in your application. In this article, we will discuss how to use it.

Motivation

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:

npm install angular

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:

npm init

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:

npm outdated

This will list for you the package(s) with current, wanted, and latest version. You can then run

npm update

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.



Senior Software Engineer with experience in C#, JavaScript, jQuery, Knockout JS, Kendo UI, HTML5, and SQL Server. Ayad has more than 20 years experience in technical training and teaching. Contributor to MrBool for about 7 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login