3 Methods to Get a List of Installed NPM Packages in Node.js

NPM (Node Package Manager) is a powerful tool used by many developers to install, manage and share packages. While working with Node.js it’s essential to keep track of the modules we have installed into our project. Listing out these modules can be a good way to keep track of them. In this article, we’ll explore some ways in which we can list NPM user-installed packages along with examples for each of these methods.

Methods to List NPM User-Installed Packages

User-installed packages may be installed both globally (meaning packages can be accessed throughout all projects on our system) and locally (packages can only be accessed in the project directory where they have been downloaded) on our system. The ways in which we can list global and local packages differ.

Some methods for listing installed packages are as follows:

  1. Using npm list
  2. Using npm list -g
  3. Using the child_processes module

Let’s look at each of these methods in detail with examples

1. Using npm list

NPM packages that have been locally for a specific project are present inside the node_modules folder under the project’s root directory.

To list locally installed packages we first navigate to our root directory and use the command:

npm list

We can also add the depth=0 flag to this command to list all the packages. NPM modules and dependencies are installed in a tree structure. The depth=0 flag is used to signify that only packages at the top level of the tree should be listed. If the depth=1 flag is used it lists all packages at the top level of the tree and their immediate dependencies.

Command to list surface-level packages using depth=0:

npm list --depth=0

Output:

Using Depth0 To List Top Level Packages

Using depth=1 we can view packages and their immediate dependencies.

Command to list top-level and one-level packages using depth=1:

npm list --depth=1

Output:

Using Depth1 To List Top Level Packages And Dependencies

2. Using npm list -g

NPM packages that have been installed globally throughout our system can be listed by using the -g (which denotes global packages) flag with the npm list command.

Command to list global packages:

npm install -g 

The depth=0 flag can be used along with global packages as well to list packages on the top level of the tree.

Command to list globally installed packages using depth=0.

npm list -g --depth=0

Output:

Using Depth0 To List Globally Installed Packages

3. Using child_process Module

A not-so-straightforward approach to listing packages that involve programming is using the child_process module.

We can install the child_process module using the command:

npm install child_process

After installation, the execSync method from the child_process module can be imported into our file using require. This method is used to synchronously run shell commands from the Node.js script.

const { execSync } = require('child_process');

const cmd = 'npm list --depth=0 --json';
const output = execSync(cmd);
const data = JSON.parse(output);

const packages = Object.keys(data.dependencies);
console.log(packages);

In the above code, the constant cmd is used to set the command that we want to run on our terminal. ‘npm list –depth=0 –json’ indicates that we want to list all packages at the top level of the package tree and return this data in JSON format using the –json flag. We use the execSync method to execute the command stored in cmd on the shell and then the output is stored in the output constant.

JSON.parse() is used to convert the output from a JSON string to a JavaScript object and is stored in the data constant. The data returned contains a property called dependencies which contain the name of the packages installed, which can be accessed using Object.keys() and stored in the packages variable.

We use console.log() to print the list of packages on the console.

Output:

List User Installed Pacakges Using Child Process Module

Conclusion

Listing packages that we have installed is a great way to manage packages globally and locally. In this article, we have looked at three different ways in which we can list NPM user-installed packages, the simplest way is using the npm list and npm list -g command. We have also looked at a more complex approach using the child_process module. We can use these methods to maintain an efficient development environment.

Reference

https://stackoverflow.com/questions/17937960/how-to-list-npm-user-installed-packages

Nandana Pradosh
Nandana Pradosh
Articles: 22