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

Package 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

1

npm update-gnpm

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.

1

npm install packageName

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

1

2

3

4

5

/*Local uninstallation*/

npm uninstall packageName

/*Local uninstallation + removal of dependencies from package.json*/

npm uninstall--save packageName

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.

1

2

3

4

5

/* To install globally*/

npm install-gpackageName

/* To uninstall globally*/

npm uninstall-gpackageName

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.

1

npm install--save grunt

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.