Open In App

Node JS NPM

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

NPM (Node Package Manager) is the default package manager for Node and is written entirely in JavaScript. Developed by Isaac Z. Schlueter, it was initially released in January 12, 2010. NPM manages all the packages and modules for Node and consists of command line client npm.

NPM gets installed into the system with installation of Node. The required packages and modules in Node project are installed using NPM. A package contains all the files needed for a module and modules are the JavaScript libraries that can be included in Node project according to the requirement of the project.

NPM can install all the dependencies of a project through the package.json file. It can also update and uninstall packages. In the package.json file, each dependency can specify a range of valid versions using the semantic versioning scheme, allowing developers to auto-update their packages while at the same time avoiding unwanted breaking changes.

Some facts about NPM:

  • At the time of writing this article, NPM has 580096 registered packages. The average rate of growth of this number is 291/day which outraces every other package registry.
  • npm is open source
  • The top npm packages in the decreasing order are: lodash, async, react, request, express.

Node Package Manager(npm)

Installing NPM:

To install NPM, it is required to install Node.js as NPM gets installed with Node.js automatically.

Install Node.js.

Checking and updating npm version

Version of npm installed on system can be checked using following syntax:

Syntax to check npm version:

npm -v
npm -v output

Checking npm version

If the installed version is not latest, one can always update it using the given syntax:

Syntax to update npm version:

npm update npm@latest -g.

As npm is a global package, -g flag is used to update it globally.

Creating a Node Project:

To create a Node project, npm init is used in the folder in which user want to create project. The npm command line will ask a number of questions like name, license, scripts, description, author, keywords, version, main file etc. After npm is done creating the project, a package.json file will be visible in project folder as a proof that the project has been initialized.

npm init

npm init

Installing Packages:

After creating the project, next step is to incorporate the packages and modules to be used in the Node Project. To install packages and modules in the project use the following syntax:

Syntax to install Node Package:

npm install package_name

Example: Installing the express package into the project. Express is the web development framework used by the Node

npm install express

Using a package in Node

To use express in the Node, follow the below syntax:

Syntax to use Installed Packages:

var express = require('express');
npm install express

Installing express module

Installing a Package Globally

To install a package globally (accessible by all projects in system), add an extra -g tag in syntax used to install the package. Installing nodemon package globally.

Syntax to Install Packages Globally:

npm install nodemon -g
npm install nodemon -g global module install example

Installing nodemon package globally

Controlling where the package gets installed:

To install a package and simultaneously save it in package.json file (in case using Node.js), add –save flag. The –save flag is default in npm install command so it is equal to npm install package_name command.

Example:

npm install express --save

Usage of Flags:

  • –save:
    flag one can control where the packages are to be installed.
  • –save-prod :
    Using this packages will appear in Dependencies which is also by default.
  • –save-dev :
    Using this packages will get appear in devDependencies and will only be used in the development mode.

Note: If there is a package.json file with all the packages mentioned as dependencies already, just type npm install in terminal

Save Directory of Installed Modules

NPM installs the dependencies in local mode (Default) which go to the node_modules directory present in the folder of Node application. To see all the locally installed modules use npm ls command.

Uninstalling Packages:

To uninstall packages using npm, follow the below syntax:

Syntax to uninstall packages:

npm uninstall

Example: To uninstall the express package

npm install package_name example

Uninstalling express

Syntax to uninstall Global Packages:

npm uninstall package_name -g

Using Semantic Versioning to manage packages:

versioning major minor patch explanation

  • To install a package of a specific version, mention the full and exact version in the package.json file.
  • To install the latest version of the package, mention “*” in front of the dependency or “latest”. This will find the latest stable version of the module and install it.
  • To install any version (stable one) above a given version, mention it like in the example below: “express”:”^4.1.1″. in package.json file. The caret symbol (^) is used to tell the npm to find a version greater than 4.1.1 and install it.


Last Updated : 27 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads