Overview Of Node Package Manager (NPM)

Introduction

Using Node.js, it is possible to write a JavaScript application directly on Server. Node.js is written in C++ and built on Google’s V8 JavaScript runtime.

NPM stands for Node Package Manager. It's an online repository of node packages that can be quickly and programmatically installed from the command line, with the npm command line interface that comes with Node.

NPM can install packages in local or global mode. In local mode, it installs the packages in npm_modules directory in the root of current working directory. Global packages are installed in C:\Users\{user}\AppData\Romaing\npm\node_modules under Windows OS or in usr/local/lib/node_modules under Linux.

Note - The following examples are developed in Node JS, on Windows.

Change location of Global Packages

The below command gives information about the current location of global packages.

  1. npm config get prefix  


The location of prefix needs to be changed in order to change the location of global packages.
 
  1. npm config set prefix c:\npm_global  


With this configuration change, we can change the location where the global packages are installed. It also creates .npmrc file in C drive.

List Global packages

The below command gives information about the installed global packages.
  1. npm list  


The above output is very detailed. This can be changed using --depth 0 option.



Insall packages in Global mode

--global flag is used to install package in global mode. This can be abbreviated to –g. Below command will install UglifyJS (JavaScript Minification Tool).
  1. npm install uglify-js --global  


Insall packages in Local mode

Packages installed without using global flag are local. Package will be installed in parent working directory in npm_modules directory. It is possible to install different versions of the same package in different directory.
  1. npm install express  


Insall specific version of packages

To install specific version of the package, @ is used to specify the version number. Below command is used to install underscore version 1.7.0.
  1. npm install [email protected]  


Uninstall NPM Packages

NPM package can be removed or uninstalled.

To uninstall global package, --global flag is used.



Local package is removed without specifying global flag.



Update NPM Packages

Installed package can be updated to specific version or latest version using below command.
  1. npm update underscore  


Search NPM Packages

Search command is used to search for the package with the name from the repository.

Below command will list all the packages with underscore like name.
  1. npm search underscore  




NPM Cache

When NPM installs package, it keeps a copy. So when same package is being installed next time, it does not need to hit the network. The copies are stored in the below directory under Windows OS.

C:\Users\{user}\AppData\Roaming\npm-cache
  1. npm cache clean  
The directory will get cluttered with different versions of packages over time. So it is good idea to clean it periodically. Below command is used to clean NPM Cache.
 


Similar Articles