NPM: View the dependency tree of a project or an NPM module

avatar
Borislav Hadzhiev

Last updated: May 25, 2023
4 min

banner

# Table of Contents

  1. NPM: View the dependency tree of a project
  2. NPM: View the dependency tree of a specific package
  3. NPM: View the dependency tree of a specific package using npm-remote-ls
  4. NPM: View the dependency tree of a specific package using howfat
  5. Checking why a package is installed

# NPM: View the dependency tree of a project

If you need to view the NPM dependency tree of your project:

  1. Open your terminal in your project's root directory (where your package.json file is).
  2. Issue the npm list command.
shell
npm list

view npm dependency tree of project

The npm list command will list the installed packages and their versions.

If you also want to print the dependencies of each package, set the --all flag.

shell
npm list --all

npm view dependency tree of project and dependencies

As shown in the screenshot, the command prints the packages in your project and their versions and the dependencies of each package.

Note that you can also use the npm ls command to achieve the same result.

shell
npm ls npm ls --all

The npm list command is an alias for npm ls.

If you only need to print the dependency tree for your production packages (the ones in your dependencies and not in devDependencies), set the --omit flag to dev.

shell
npm list --all --omit=dev

only view dependency tree for production packages

You can also set the --depth option to specify the depth of the generated dependencies tree.

The syntax for the command is npm list --depth=[depth].

shell
npm list --depth=1

generate dependencies tree with depth

I set the --depth option to 1 to generate a dependencies tree that is 1 level deep.

If you use yarn instead of npm, you can use the yarn list command.

shell
# list all dependencies in the tree yarn list # set the depth of the dependencies tree to 0 yarn list --depth=0

# NPM: View the dependency tree of a specific package

To view the dependency tree of a specific package, use the npm view <YOUR_PACKAGE> dependencies command.

shell
npm view <YOUR_PACKAGE> dependencies

view dependency tree of specific package

Make sure to replace the <YOUR_PACKAGE> placeholder with the name of the actual package, e.g. npm view axios dependencies.

You can also view the dependencies of a specific version of a package.

shell
npm view axios@1.4.0 dependencies

view dependencies for specific version of a package

The example prints the dependencies of the axios package for version 1.4.0.

Note that the npm view <PACKAGE> dependencies command prints the direct dependencies of the package, not the whole tree.

# NPM: View the dependency tree of a specific package using npm-remote-ls

If you want to view all dependencies of a package (the whole tree), use the npm-remote-ls package.

shell
npx npm-remote-ls <YOUR_PACKAGE>

view dependency tree of npm package

Make sure to replace the <YOUR_PACKAGE> placeholder with the name of the actual package.

You can also install the npm-remote-ls package globally to not have to use it with the npx prefix.

shell
npm install npm-remote-ls -g

If you get a permissions error on macOS or Linux, prefix the command with sudo.

If you get a permissions error on Windows, open CMD as an administrator and rerun the command.

Now you can use the command directly, without the npx prefix.

shell
npm-remote-ls <YOUR_PACKAGE>

using command without npx prefix

As shown in the screenshot, the react package has 2 dependencies in its tree.

However, if I use the npm view react dependencies command, only 1 dependency is shown.

shell
npm view react dependencies

using npm view package dependencies command

This is because the npm view <PACKAGE> dependencies command prints only the direct dependencies on the given package.

Whereas the npm-remote-ls module prints the whole dependencies tree.

You can also print the dependencies tree of a specific version of a package.

shell
npx npm-remote-ls react@18.2.0

print dependencies tree of specific version of package

The example prints the dependencies tree of the package react at version 18.2.0.

# NPM: View the dependency tree of a specific package using howfat

There is also a howfat module that you can use to view the dependency tree of a specific package.

shell
npx howfat <YOUR_PACKAGE>

view dependencies tree of package using howfat

You can also specify the version of the package for which you want to view the dependencies tree.

shell
npx howfat react@18.2.0

The command prints the dependencies tree for react version 18.2.0.

print dependencies tree for specific version using howfat

You can view more examples of using the howfat package on its NPM page.

# Checking why a package is installed

If you use yarn, you can also use the yarn why <PACKAGE> command to check why a module is installed.

You might have to run the yarn install command to generate a yarn.lock file if you haven't got one.
shell
yarn install

Now issue the yarn why <YOUR_PACKAGE> command.

shell
yarn why <YOUR_PACKAGE>

issue yarn why command

As shown in the screenshot, the specified package exists because axios depends on it.

Here is another example.

issue yarn why on main package

The screenshot shows that the axios package exists because it is specified in the dependencies object of the package.json file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.