NPM – Node Package Manager

N

In this article, you will learn about NPM Package Manager in Node. You may think, why a dedicated node package manager comes with Node. The first answer is Yes, Node JS is based on Javascript and its main worth is due to the large number of packages bundled in it. To manage all these packages, a separate service help NPM helps to perform the following two main functionalities.

Functionalities of NPM in Node JS

Provides command-line utility to install/uninstall node packages. Management of version and dependency and Node JS packages.

Some of the Node packages are officially available at search.nodejs.org. NPM is responsible for managing this online repository of packages and modules.

How to install and check NPM version?

To install Node Package Manager in your Node JS project, follow the steps given below.

  1. Open the folder where you have placed your Node JS project.
  2. Open the terminal in that folder. Or you can simply use the visual studio code terminal.
  3. Run following command in the terminal.
npm install

Done! It will take some time depending on the speed of your internet. It needs to download and install some files/packages.

Note: If you are using Node v0.6.3, then you don’t need to perform the above steps to install Node Package Manager. It comes in the installable bundles of v0.6.3 and above.

To check the version of Node Package Manager, run the following command in the terminal.

npm --version

How to update NPM from the old version to the latest?

If you are using the old version of NPM, it’s really easy to update it to the latest version. Simply, run the following command.

sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 /usr/lib/node_modules/npm

How to use Node Package Manager to install packages/modules

You need to run the commands in the terminal to install/uninstall packages in NPM. Run the following command to install a new module in Node using NPM.

$ npm install <Module Name>

For Instance – the following command will install a new module express in the Node.

$ npm install express

Now, you can use the express module in your Node project. For example, the HTTP client instance is available in the express module, which we have installed above using the npm install express command.

Node Package Manager- About Local vs Global Installation

Local modules are the modules, which are accessible locally in the project. They are placed in the directory named node_modules. It contains a subdirectory for each individual module, installed by NPM.

Just like the express module we have installed above as an example of the NPM package. If it is the first package you are installing in your project, it will create node_packages in your project automatically and place the module in its sub directly.

Use the npm ls command to list down all the modules installed locally.

$ ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules

Global Modules are installed in the system directory. These dependencies are accessible inside the command line interface of the current Node JS project but we cannot import them into our project using the required directive. To install a global module using NPM, use the following command.

$ npm install express -g

To check the globally installed modules, rin the following command

$ npm ls -g

How to uninstall a module in Node Package Manager – NPM

To uninstall the node module using NPM, just run the following command.

$ npm uninstall express

You can verify by running the following command or going through the node_modules directory.

$ npm ls

How to update a module in Node Package Manager

To update the NPM module, to the latest version, run the following command in the terminal.

$ npm update express

You can search any Node Package by following the command.

$ npm search express

What is the Package.json file in NPM?

Package.json is also known as the heart of the Node JS project because it contains all the properties of the applications we performed. The express package we have installed in this tutorial should be present in node_modules/express directory.

Reference to the official Node Package Manager documentation

About the author

Tutor Network Team

2 comments

Tutor Network