Package Manager – why use npm? How to use npm?

NPM LogoPackage manager manages assets (install, upgrade, remove, manages dependencies) used in a project. NPM is used to install node packages (such as underscore, grunt, gulp, jsHint etc.) from npm registry. NPM makes it easy for JavaScript developers to share and reuse their code. You just need to publish package to the registry. These reusable codes are called packages or modules in npm. Node.js modules are intended for public use, but it can be private for an individual or an enterprise.

Why use npm?

Using the npm, you can share and reuse the code in different projects. Apart from sharing and reusing your own code, you can use n-numbers of packages registered (like grunt, angular, gulp etc.) in the npm registry. NPM helps to install, remove, update and maintain the large project dependencies easily.

Installing and updating npm

NPM is a package manager that comes with Node.js. However npm gets updated more frequently than node. Make sure you have updated npm installed. If not, either download node.js again or run the command

Difference between Local and global installation of a package

There are two ways of installing packages – locally and globally. If you want to depend on the package from your own module (like jquery, angular etc.), then you should install locally. On the other hand if you want to use it as command line tool (such as grunt, gulp, susy) then you want to install it globally.

npm – local installation of a package

By default, npm install packages locally. A package can be downloaded or installed with the command.

This will create a node_modules directory and download the package into that directory.

npm – uninstallation of a local package

To remove the package from your node_modules directory use command npm uninstall and to remove from the dependencies in package.json. You need to add --save flag

npm – global installation of a package

If the required package has to be used as command line tool, like grunt, gulp, then it should be installed globally. To download a package globally -g flag has to be added with the command.

Manage dependencies with package.json

This is the other way of managing npm package locally and maintains the dependencies for the project. You need package.json to maintain the dependencies in the project.

npm – Create package.json

Create package.json at the root of the project or you can run the command npm init to create the package.json. This file contain list of dependencies and project specific metadata like project name, description version etc.

npm – Manage Dependencies

Now, either you can list all your package dependencies in the package.json manually and run the command npm install it will install all the dependencies. Or you can use --save flag while installing a package. It will add the dependencies in the package.json.

Managing npm Cache

A copy is cached in the .npm directory when a package is installed. So, that when you want to install that package, it doesn’t hit the network again. Over a period of time, this directory will cluttered with old packages. So, it’s better to clean it occasionally.

Some important reference links

Author: Gopal Juneja

A UX/UI enthusiast living in India Delhi having 18+ years of industry experience to use in beautiful design and handsome front end coding.

Leave a Reply

Your email address will not be published. Required fields are marked *