Open In App
Related Articles

Node.js | NPM (Node Package Manager)

Improve Article
Improve
Save Article
Save
Like Article
Like

NPM (Node Package Manager) is the default package manager for Node.js 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.js and consists of command line client npm. It gets installed into the system with installation of Node.js. 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:

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:

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:

npm install package_name

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

npm install express

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

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

Installing express module

Example: 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.

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

By –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.
Example: npm install node-color –save-dev

npm install --save-dev example. development dependency installed

–save-dev example

If there is a package.json file with all the packages mentioned as dependencies already, just type npm install in terminal. npm will look at package.json file and install all the dependencies according to their mentioned versions. This command is typically used when a Node project is forked and cloned. The node_modules being a big folder is generally not pushed to a github repo and the cloner has to run npm install to install the dependencies.

Note: 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:

npm uninstall 

Example: To uninstall the express package

npm install package_name example

Uninstalling express

To uninstall global packages, follow the below syntax:
Syntax:

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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 12 Jul, 2023
Like Article
Save Article
Similar Reads
Related Tutorials