The Wayback Machine - https://web.archive.org/web/20240911044405/https://www.geeksforgeeks.org/node-js-npm-node-package-manager/
Open In App

Node JS NPM

Last Updated : 06 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

Similar Reads

Difference between npm i and npm ci in Node.js
The following difference covers how npm i and npm ci command are different from each other and their functioning. The npm which is called a node package manager which is used for managing modules needed for our application. npm i: The npm i (or npm install) is used to install all dependencies or devDependencies from a package.json file. Syntax: npm
2 min read
Difference between npm install and npm update in Node.js
NPM is like a powerhouse for Node.js that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS. How to install Node on the machine? Refer to this article. NPM has 580096 registered packages. The average rate of growth of this nu
5 min read
How to solve npm error npm ERR! code ELIFECYCLE ?
In order to solve the "npm ERR! code ELIFECYCLE " error which is a very common type of error that occurs during npm operation on our command prompt or terminal such as installing npm or an npm package, follow the steps given below : Terminal output of the error : Follow below steps to fix the error : Step 1 : In our first step we will try cleaning
2 min read
How to Get a List of Globally Installed NPM Packages in npm ?
Learning how to retrieve a list of globally installed NPM packages is essential for developers managing their Node.js environment. Utilizing npm, the Node Package Manager, enables users to efficiently manage packages across projects. This article will guide you through getting access to global NPM packages, which will further help you to enhance yo
2 min read
Axios npm - How to Install Axios npm in Terminal?
Axios is a popular JavaScript library for making HTTP requests in web applications. It simplifies the process of sending asynchronous requests and handling responses. It is commonly used with Node.js and in browser-based projects and can be installed via NPM (Node Package Manager). In this article, we will explore the step-by-step process of instal
2 min read
npm cache clean - How to Clear the Cache in NPM ?
Clearing the cache in NPM (Node Package Manager) is very important. It helps to resolve various issues and ensures smooth functioning of the Node.js projects. The NPM cache stores downloaded packages and their metadata, which can sometimes become corrupted or outdated, leading to installation problems, dependency conflicts, or other issues. Table o
5 min read
Remove NPM - npm uninstall
To remove npm (Node Package Manager) from your macOS system, you can't use npm uninstall since npm itself doesn't support uninstalling itself. Instead, you need to remove it manually along with Node.js. Table of Content What is NPM?What is NPM remove?Installing a package using npmUninstalling a Package using npmRemove npmRemove a Dev DependencyWhat
2 min read
Node.js NPM arraybuffer-to-string Module
NPM(Node Package Manager) is a package manager of Node.js packages. There is an NPM package called arraybuffer-to-string used to decode array buffers in actual strings. The package not only converts the buffer to 'utf8' string but also it converts the buffer to many forms like base64 encoded string, a hex-encoded string that we use in many contexts
4 min read
Node.js NPM string-to-arraybuffer Module
NPM(Node Package Manager) is a package manager of Node.js packages. There is a NPM package called 'shortid' used to short non-sequential url-friendly unique ids. Command to install: npm install string-to-arraybuffer Syntax to import the package in local file const str2ab = require('string-to-arraybuffer') Syntax to convert string to an array buffer
5 min read
Node.js NPM shortid Module
NPM(Node Package Manager) is a package manager of Node.js packages. There is a NPM package called 'shortid' used to create short non-sequential url-friendly unique ids. By default, it uses 7-14 url-friendly characters: A-Z, a-z, 0-9, _-. It Supports cluster (automatically), custom seeds, custom alphabet. It Can generate any number of ids without du
6 min read
Node.js NPM uuid
NPM(Node Package Manager) is a package manager of Node.js packages. There is an NPM package called 'shortid' used to create short non-sequential url-friendly unique ids. Unique ids are created by Cryptographically-strong random values that's why it is very secure. It has support for cross-platform like Node, React Native, Chrome, Safari, Firefox, e
6 min read
How to install modules without npm in node.js ?
We can install modules required for a particular project in node.js without npm, the recommended node package manager using yarn. Yarn is a wonderful package manager. Like npm, if you have a project folder with package.json containing all the required dependencies mentioned for the project, you can use yarn to install all the dependencies. 1. How t
3 min read
How to list npm user-installed packages in Node.js?
What is Node.js? Node.js is an open source and cross-platform runtime environment for executing JavaScript code outside of a browser. Click here for more. What is npm? Here, "npm" stands for "Node Package Manager" which is the package manager for Node.js and serves as a command-line utility for interacting with the npm to install different online p
2 min read
What are Request and Cheerio in Node.js NPM ?
In this article, we are going to learn about that request and the cheerio library of node.js with their installation. Request Library: The request module is a very popular library in node.js. It helps in sending the request to the external web application to make the conversation between the client and the server by using the HTTP request. HTTP(or
3 min read
How to Generate Borderless Table using 'text-table' NPM Module in Node.js ?
'text-table' is a very useful NPM module that can be used to generate borderless tables from the array of data. It's a very simple module and you can easily create tables that are well-aligned, simple to read, etc. One of the main advantages of this module is its simplicity. With just a few lines of code, we can create a well-structured table and w
3 min read
How to install the previous version of Node and npm?
Installing a specific version of Node.js and npm can be essential for compatibility reasons or to work with legacy projects. In this guide, we’ll go through the steps to install an older version of Node.js and npm on your system. What is NodeJS?Node is a JavaScript runtime(server-side) built on the V8 JavaScript engine of Google Chrome. It was deve
2 min read
Top npm packages for node
NodeJS has become an effective tool for server-side development, thanks to its extensive ecosystem of npm packages. These packages offer a wide range of functionalities, from web frameworks to utility libraries, enabling users to build robust and scalable applications efficiently. In this article, we'll delve into seven top npm packages for NodeJS
4 min read
How To Use Node Modules with npm and package.json
NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This article will guide you through the process of usi
3 min read
How to Install NPM FS in Node JS ?
The file system module allows you to work with the file system on your computer NodeJS includes the fs module, to communicate with file systems. It provides functionality for interacting with the file system, such as reading from and writing to files, creating and removing directories, etc. The File System module in NodeJS is one of the most import
4 min read
How to Install Node & Run npm in VS Code?
Node is an open-source, server-side JavaScript runtime environment built on the V8 engine. It allows developers to execute JavaScript code outside of a web browser, enabling the development of scalable and efficient network applications. Known for its event-driven architecture, NodeJS is widely used for building fast, lightweight, and highly respon
2 min read
Node.js nodemon npm Module
The nodemon npm Module is a module that develop node.js based applications by automatically restarting the node application when file changes in the directory are detected. Nodemon does not require any change in the original code and method of development. Advantages of Using nodemon Module: It is easy to use and easy to get started.It does not aff
1 min read
Downloading and Installing npm and Node.js
The Node.js can be installed in multiple ways on a computer. The approach depends on the existing development environment in the system. There are different package installers for different environments. You can install Node.js by grabbing a copy of the source code and compiling the application. Another way of installing Node.js is by cloning the G
2 min read
How to Find the Version of an Installed NPM Package in Node.js ?
NPM is the default package manager for Node.js. NPM manages both internal and external packages or modules used in various Node.js applications. The default packages in NPM may not fulfil all the needs of a developer, so we often require external packages. These can be installed either locally in a specific directory on our machine or globally, mak
2 min read
15 npm Commands that Every Node.js Developer Should Know
NPM stands for Node Package Manager and it is the package manager for the Node JavaScript platform. It put modules in place so that node can find them, and manages dependency conflicts intelligently. Most commonly, it is used to publish, discover, install, and develop node programs. Some Important npm commands every developer should know are: NPM I
2 min read
How to Change npm start Script of Node.js ?
In Node.js, npm (Node Package Manager) provides a convenient way to manage project scripts through the scripts field in the package.json file. By default, the npm start command is used to start your Node.js application. However, you might need to customize this command to suit specific requirements, such as running a different entry point, adding e
3 min read
How to Generate Random and Unique Password in Node.js using 'generate-password' NPM Module?
The generate-password module provides a simple and flexible way to generate random passwords. It offers various options to customize the length, complexity, and character set of the generated passwords. This makes it a great tool for developers who need to implement password generation in their applications. Steps to Setup ProjectStep 1: Make a fol
2 min read
How to Resolve npm Command Not Found Error in Node.js
The npm Command Not Found error typically occurs when your system is unable to locate the npm (Node Package Manager) command. Node JS is a powerful platform for building server-side applications using JavaScript. Alongside Node JS, npm (Node Package Manager) is used commonly for managing dependencies and packages within NodeJS projects. We will dis
4 min read
How to Update Node.js and NPM to the Latest Version?
Updating Node.js and NPM to the latest version ensures the newest features, performance improvements, and security updates. This article will guide you through the steps to update Node.js and npm to the latest version on various operating systems, including Windows, macOS, and Linux. To update Node and NPM to latest versions we can use various meth
3 min read
Steps to Create and Publish NPM packages
In this article, we will learn how to develop and publish your own npm package (also called an NPM module). There are many benefits of NPM packages, some of them are listed below: Reusable codeManaging code (using versioning)Sharing code The life-cycle of an npm package takes place like below: 1. Setup a Project: Setting up a project is required be
7 min read
How to use External Modules and NPM in a project ?
Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into multiple files. Node.js adopts this CommonJS forma
3 min read