Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Installing, Listing and Uninstalling packages using npm

You are prob­a­bly used to using npm to install and update pack­ages by issu­ing the npm install and npm update com­mands. How­ever, you ought to know a lit­tle bit more about the dif­fer­ent options avail­able to you dur­ing pack­age install.

Installing pack­ages using npm

When I first began, i just installed pack­ages using the command

npm install packagename

How­ever that did’nt last long and I real­ized that you need to be able to save the pack­ages that you need in your projects so that you have to remem­ber the names of all the pack­ages you needed to make your project run — aka, your project’s dependencies.

There are two ways in which you can achieve this

  1. Within your package.json file cre­ate a key called ‘depen­den­cies’ whose value is map that spec­fies your depen­dency name and its version.

    {
        dependencies: {
          "express": "4.0.1"
        }
    }
    

    Although this might seem pretty straight­for­ward at first, its a pain to have to know the ver­sion of each pack­age that you intend to install.

  2. The sec­ond and smarter option, espe­cially if you just start­ing out, is to run the npm install com­mand with the –save com­mand as such

        npm install express --save
    

    The above com­mand not only installs the lat­est ver­sion of the pack­age ‘express’ for you but it also updates your package.json file and adds the pack­age name and the installed ver­sion num­ber to your depen­den­cies map.

This way, the next time you clone your pack­age, or delete your node_modules folder and then issue the npm install com­mand, npm will install exact same ver­sions of your pack­ages that were spec­i­fied in your pack­age json there­fore pre­vent­ing your code from break­ing due to updates to packages.

The next inter­est­ing hap­pens when you real­ize that you actu­ally have two kinds of pack­ages — ones that are required for your appli­ca­tions to run and oth­ers that you use as tools that help you dur­ing devel­op­ment — such as jshint, grunt, grunt tasks, mini­fi­ca­tion, sass com­pil­ers etc etc.

Since these pack­ages are only required by the devel­oper, they dont need to be installed by some­one else that might use your pack­age in the future. The rea­son why it is impor­tant to make this dis­tinc­tion is because when some­one else who men­tions your pack­age as depen­dency in their project issues an npm install, npm will also go ahead and install all the pack­ages spec­i­fied in your ‘depen­den­cies’. npm can­not dis­tin­guish between manda­tory pack­ages and development-only pack­ages. Well at least not unless you tell it to.

The cor­rect way to spec­ify devel­op­ment only pack­ages is by list­ing them in your package.json under the key ‘devDe­pen­den­cies’. And just like before, you don’t need to remem­ber the pack­age ver­sion num­bers of these pack­ages. You can install them by sim­ply run­ning the npm install com­mand with the –save-dev option as shown

npm install grunt --save-dev

Just like the –save option, the –save-dev option updates your package.json file but this time it adds an entry to your devDe­pen­den­cies map thereby help­ing you make a clear dis­tinc­tion between required-to-run pack­ages and development-only packages.


List­ing installed pack­ages using npm

If at any point you want to see the list all the pack­ages that are installed and their depen­den­cies, you can do that by the command

npm list

If the tree it spits out is just too much detail, you can spec­ify the depth upto which you are inter­ested by the command

npm list --depth=0

where ‘0’ is the first level of depedencies.

You can also run the above com­mands with the –g option to get details of all your glob­ally installed modules.

npm list -g --depth=0

Unin­stalling pack­ages using npm

If at any time, you want unin­stall a mod­ule, you can do that by the command

npm uninstall packagename

And just like the npm install com­mand, the unin­stall com­mand also takes accepts –g, –save and –save-dev to remove global mod­ules and update the pack­age depen­den­cies or devDe­pen­den­cies respectively.


Remov­ing unused pack­ages from node_modules

Some­times after installing pack­ages, you real­ize that you dont really need some of them and you delete those entries from your package.json. Although those pack­ages will not be installed again on run­ning an npm install, you still need to remove them from your node_modules folder at least once. To remove all such unused pack­ages from your node_modules, you can run the command

npm prune

Remov­ing all devDe­pen­den­cies from node_modules

If you just want to delete all of your installed devDe­pen­den­cies while retain­ing their entry in the package.json file, you can do that using the command

npm prune --production

After this com­mand, the only pack­ages left in your node_modules direc­tory will be the ones that are spec­i­fied in your depen­den­cies map.


Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...