Open In App

How To Get a List of Globally Installed NPM Packages in NPM?

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To get a list of globally installed NPM packages, there are several methods you can use depending on the level of detail and format you need. Below, we’ll cover two main approaches that utilize different commands to retrieve this information: npm list and npm ls.

Approach 1: Using 'npm list' command:

To get a list of globally installed npm packages, we can use npm list command in our terminal or command prompt. The npm list command is used to list installed npm packages.

  • -g flag indicates that you want to list globally installed packages.
  • --depth=0 option specifies the depth of the dependency tree to display. Setting it to 0 ensures that only the top-level packages are listed without their dependencies.

Syntax:

npm list -g --depth=0

Output:

grghthh
Get a List of Globally Installed NPM Packages in npm

This will show the name and version of each globally installed package. The tree-like structure makes it easy to identify the packages you have installed globally.

Approach 2: Using 'npm ls' command

Another approach to list globally installed packages is by using the npm ls command. This command works similarly to npm list but can provide a cleaner, parseable output, which is useful when you need to process the results programmatically or store them in a file.

  • -g flag indicates that you want to list globally installed packages.
  • --depth=1 option limits the depth of the dependency tree. In this case, --depth=1 limits the output to only the top-level packages and their direct dependencies.

Syntax:

npm ls -g --depth=1

Output

Screenshot-2024-03-13-021336
displaying top-level packages along with their immediate dependencies

This output shows the absolute paths to the globally installed packages, which is particularly useful for scripting or saving the list to a file for later review.

Conclusion

Both npm list and npm ls commands are powerful tools for managing globally installed NPM packages.

  • Use npm list when you want a more detailed, tree-like view of installed packages.
  • Use npm ls --parseable when you need a cleaner, simpler format, particularly for scripting or storing the list in a file.

These commands make it easy to view, update, or uninstall global packages, helping you manage your Node.js development environment more efficiently.


Similar Reads