Understanding NPM: The Node.js Package Manager

Understanding NPM: The Node.js Package Manager

NPM, which stands for Node Package Manager, is a command-line tool for installing Node.js packages and managing their versions and dependencies. Additionally, NPM provides a robust online repository where developers can explore and access a vast collection of Node.js packages and modules. This repository serves as a centralized hub for sharing, managing, and discovering reusable code components, making development more efficient.

The NPM tool comes with the Node.js binaries for all OS platform bundles if you are using a more recent version of Node.js (version 0.6.0 or later). Verify the NPM version in the command terminal.

PSC:\Users\mlath> npm -v
10.1.0

If you are using an earlier version of NPM, you must use the following command to update it to the most recent version.

Installing npm -g

Keep in mind that there are YARN and PNPM tools available as NPM substitutes. In safe offline situations or across machines, YARN manages dependencies reliably and expedites package installations. Another quick and disk-space-efficient package manager for Node.js packages is called PNPM (Performant NPM).

Installing any external packages that your Node.js application need must come from the NPM repository. There are two ways to install NPM packages: locally and globally. A package is installed locally by default.

Install the package locally.

Any Node.js module can be installed using this straightforward 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

Now you can use this module in your js file as following −

var express =require('express');

A package installed in the node_modules directory, which is located in the folder containing the Node application, is said to be installed in local mode. The require() method provides access to locally deployed packages. To include a dependent entry in your application’s package.json, use –save at the conclusion of the install command.

In Node.js projects, dependencies are managed using the package.json file, which is a JSON file. It includes details about the project, including its name, dependencies, and version. The npm package manager installs and manages dependencies using the package.json file.

The package.json file is typically located in the root directory of a Node.js project. It can be created by running the npm init command.

Create a new folder for a new Node.js project, and run pnm init command inside it −

PSD:\nodejs\newnodeapp> npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.packagename:(newnodeapp) newnodeapp
version:(1.0.0)description: Test Node.js App
entry point:(index.js)
test command:
git repository:keywords: test, nodejs
author: mvl
license:(ISC)

About to write to D:\nodejs\NewNodeApp\package.json −

{"name":"newnodeapp","version":"1.0.0","description":"Test Node.js App","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["test","nodejs"],"author":"mvl","license":"ISC"}

Now, if we install express package into this package locally in this project, use the following command, it also adds dependency entry into the package.json.

D:\nodejs\newnodeapp>npm install express –save

The package.json file in this folder will be updated to −

{"name":"newnodeapp","version":"1.0.0","description":"Test Node.js App","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["test","nodejs"],"author":"mvl","license":"ISC","dependencies":{"express":"^4.18.2"}}

The package folder’s node_modules subfolder is where the express package code will be stored.

Running npm install (without the package name in front of it) will install all of the project dependencies at once if you have already placed them all in your package.json file.

To add the package as DevDepndency, you can also use the -save-dev argument in the npm install command.

  • Installing and adding the entry to the package is done with –save-dev. file devDependencies in JSON
  • Installing using –no-save does not include the entry in the package. Dependencies in JSON files
  • Installing and adding the entry to the package is done with –save-optional. optionalDependencies json file

Installing optional dependencies will not be possible with –no-optional.

You can also use the flags’ shorthands −

  • -S: –save
  • -D: –save-dev
  • -O: –save-optional

The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production.

Install Package Globally

Managing NPM Packages in Node.js

Global Installation of Packages

When you install a package globally using NPM, it is stored in a system directory and can be accessed from the command line interface (CLI) across different Node.js projects. However, globally installed packages cannot be directly imported using the require() function in a Node.js application.

For example, installing the Express module globally can be done using the following command:

npm install express -g

This process installs the module at a system-wide level. The installation location varies based on the operating system:

  • On Linux, global packages are stored in the /usr/lib/node_modules directory.
  • On Windows, they are placed in C:\Users\your-username\AppData\Roaming\npm\node_modules.

Updating a Package in Node.js

To update a package installed locally within your Node.js project, navigate to your project folder in the terminal or command prompt and use the update command:

npm update <package-name>

For instance, updating the ExpressJS module to the latest version can be done as follows:

npm update express

Upon successful execution, the output will indicate that the package is up to date and provide an audit summary of the dependencies. If certain packages require funding, a message will suggest running npm fund for details.

Uninstalling Packages

If a package is no longer needed, it can be removed from the project dependencies using the uninstall command:

npm uninstall <package-name>

For example, to remove the ExpressJS module from a project, run:

npm uninstall express

This command will remove the package along with its dependencies, ensuring the project remains lightweight and only contains necessary modules. After the process, an audit summary will be displayed, highlighting the number of packages removed and verifying if there are any security vulnerabilities.

By efficiently managing package installation, updates, and removals, developers can maintain a clean and optimized development environment while ensuring that projects use the latest and most secure dependencies.


The package entry will also be removed from the list of dependencies in the package.json file.

It might be helpful:

What’s New in Node.js 21/22? 

Best Practices for API Design in Full-Stack Development

What is REST API with an example?

admin
admin
https://www.thefullstack.co.in

Leave a Reply