Be the first user to complete this post

  • 0
Add to List

Installing, Listing and Uninstalling packages using npm

You are probably used to using npm to install and update packages by issuing the npm install and npm update commands. However, you ought to know a little bit more about the different options available to you during package install.

Installing packages using npm

When I first began, i just installed packages using the command
npm install packagename
However that did'nt last long and I realized that you need to be able to save the packages that you need in your projects so that you have to remember the names of all the packages 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 create a key called 'dependencies' whose value is map that specfies your dependency name and its version.
    {
        dependencies: {
          "express": "4.0.1"
        }
    }
    
    Although this might seem pretty straightforward at first, its a pain to have to know the version of each package that you intend to install.
  2. The second and smarter option, especially if you just starting out, is to run the npm install command with the --save command as such
        npm install express --save
    
    The above command not only installs the latest version of the package 'express' for you but it also updates your package.json file and adds the package name and the installed version number to your dependencies map.
This way, the next time you clone your package, or delete your node_modules folder and then issue the npm install command, npm will install exact same versions of your packages that were specified in your package json therefore preventing your code from breaking due to updates to packages. The next interesting happens when you realize that you actually have two kinds of packages - ones that are required for your applications to run and others that you use as tools that help you during development - such as jshint, grunt, grunt tasks, minification, sass compilers etc etc. Since these packages are only required by the developer, they dont need to be installed by someone else that might use your package in the future. The reason why it is important to make this distinction is because when someone else who mentions your package as dependency in their project issues an npm install, npm will also go ahead and install all the packages specified in your 'dependencies'. npm cannot distinguish between mandatory packages and development-only packages. Well at least not unless you tell it to. The correct way to specify development only packages is by listing them in your package.json under the key 'devDependencies'. And just like before, you don't need to remember the package version numbers of these packages. You can install them by simply running the npm install command 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 devDependencies map thereby helping you make a clear distinction between required-to-run packages and development-only packages.

Listing installed packages using npm

If at any point you want to see the list all the packages that are installed and their dependencies, you can do that by the command
npm list
If the tree it spits out is just too much detail, you can specify the depth upto which you are interested by the command
npm list --depth=0
where '0' is the first level of depedencies. You can also run the above commands with the -g option to get details of all your globally installed modules.
npm list -g --depth=0

Uninstalling packages using npm

If at any time, you want uninstall a module, you can do that by the command
npm uninstall packagename
And just like the npm install command, the uninstall command also takes accepts -g, --save and --save-dev to remove global modules and update the package dependencies or devDependencies respectively.

Removing unused packages from node_modules

Sometimes after installing packages, you realize that you dont really need some of them and you delete those entries from your package.json. Although those packages will not be installed again on running an npm install, you still need to remove them from your node_modules folder at least once. To remove all such unused packages from your node_modules, you can run the command
npm prune

Removing all devDependencies from node_modules

If you just want to delete all of your installed devDependencies while retaining their entry in the package.json file, you can do that using the command
npm prune --production
After this command, the only packages left in your node_modules directory will be the ones that are specified in your dependencies map.



Also Read:

  1. Unit test your Nodejs RESTful API using mocha
  2. Setup nginx with multi domain websites running on nodejs
  3. What is an npmignore file and what is it used for
  4. Configure The 'script' tag In package.json To Run Multiple Commands