Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Node.js - NPM (Node Package Manager)

January 24th 2021   95

Node.js - NPM (Node Package Manager)
Node.js - NPM (Node Package Manager)

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