List Installed npm Packages

6 min read·Jan 1, 2025

Being able to list the packages used by a project is quite helpful as it allows developers to better understand how packages are built in general, control the size of their applications, fix package-related bugs or conflicts by downgrading or bumping package versions, replace unmaintained packages, and so on.

List installed packages

List locally installed packages

To output the list of top-level packages installed in the node_modules directory of a project, you can navigate to the project, and run the npm ls command:

$ npm ls

Example

For example, this command will show the top-level packages of the website project:

$ npm ls
website@1.0.0 /Users/rludosanu/Projects/website
├── axios@1.4.0
└── dayjs@1.11.9

List globally installed packages

To output the list of top-level packages installed globally on the system, you can use the npm ls with the -g flag:

$ npm ls -g

Example

For example, this command will show the globally installed top-level packages:

$ npm ls -g
/Users/rludosanu/.nvm/versions/node/v20.3.0/lib
├── npm@9.6.7
└── pm2@5.3.0

Show the dependency tree of installed packages

The top-level packages installed with npm often rely on other packages referred to as dependencies, which can also rely on other dependencies, and so on.

By default, the npm ls command will only show the top-level packages of the root project or the global directory.

To recursively list the dependencies of top-level packages, you can use the npm ls command with the --depth flag:

$ npm ls --depth N

Where N is a number representing the depth of the shown dependency tree.

Example

For example, this command will only show top-level packages of the website project, without their dependencies:

$ npm ls --depth 0
website@1.0.0 /Users/rludosanu/Projects/website
├── axios@1.4.0
└── dayjs@1.11.9

This command will show top-level packages and their immediate dependencies:

$ npm ls --depth 1
website@1.0.0 /Users/rludosanu/Projects/website
├─┬ axios@1.4.0
│ ├── follow-redirects@1.15.2
│ ├── form-data@4.0.0
│ └── proxy-from-env@1.1.0
└── dayjs@1.11.9

This command will show top-level packages, their dependencies, and the immediate dependencies of those dependencies:

$ npm ls --depth 2
website@1.0.0 /Users/rludosanu/Projects/website
├─┬ axios@1.4.0
│ ├── follow-redirects@1.15.2
│ ├─┬ form-data@4.0.0
│ │ ├── asynckit@0.4.0
│ │ ├── combined-stream@1.0.8
│ │ └── mime-types@2.1.35
│ └── proxy-from-env@1.1.0
└── dayjs@1.11.9

Note: Running npm ls is equivalent to npm ls --depth=0, and running npm ls --depth is equivalent to npm ls --depth=1

Show all dependencies

To show the entire dependency tree of locally and globally installed packages, you can use the --all flag as follows:

$ npm ls --all [-g]

Use the package-lock.json file

By default, the npm ls command will show the dependency tree of installed packages only.

To show the dependency tree of a project that doesn't contain a node_modules folder based on its package-lock.json file, you can use the --package-lock-only flag as follows:

$ npm ls --package-lock-only
website@1.0.0 /Users/rludosanu/Projects/website
├── axios@1.4.0
└── dayjs@1.11.9

Use the package.json file

Another way to view a project's dependency list without using the npm ls command is to display the package.json file using commands such as cat or less, and look for the dependencies, devDependencies, and peerDependencies properties within this file.

$ cat package.json
{
  "name": "website",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "NODE_ENV=development node index.js",
  },
  "dependencies": {
    "axios": "^1.4.0",
    "dayjs": "^1.11.9",
  }
}

Summary

Here's a summary of what you've learned in this lesson:

  • The npm ls command is used to list locally installed packages.
  • The npm ls -g command is used to list globally installed packages.
  • The npm ls --depth command is used to list the dependencies of installed packages by depth.
  • The npm ls --all command is used to list the entire dependency tree of installed packages.
  • The npm ls --package-lock-only command is used to list the dependency tree of a project based on its package-lock.json file.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
List Installed Packages in npm | Backend Brewery