Node JS Package Manager - NPM

Node JS Package Manager - NPM

Node Package Manager (NPM) provides two main functionalities -

  • Online repositories for node.js packages/modules which are searchable on search.nodejs.org
  • Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

NPM comes bundled with Node.js installables after v0.6.3 version. To verify the same, open console and type the following command and see the result -

npm --version

Installing Modules using NPM

// syntax
npm install <Module Name>

For example, following is the command to install a famous Node.js web framework module called express -

npm install express

Global vs Local Installation

By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require() method.

Now let's try installing the express module using local installation.

npm install express

Globally installed packages/dependencies are stored in system directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly. Now let's try installing the express module using global installation.

npm install express -g

Using package.json

package.json is present in the root directory of any Node application/module and is used to define the information of the current project and it contains list of dependencies present in your project.

As node_modules/ folder contains packages and is not a part of code base so we don't share this folder along with the source code but this folder can be regenerate using package.json by using below command in the root directory of the project

npm install

OR

npm i

In this section, we saw the learn about Node JS package manager and its Usage