Last updated: May 25, 2023
Reading time·4 min
If you need to view the NPM dependency tree of your project:
package.json
file is).npm list
command.npm list
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.
npm list --all
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.
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
.
npm list --all --omit=dev
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]
.
npm list --depth=1
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.
# list all dependencies in the tree yarn list # set the depth of the dependencies tree to 0 yarn list --depth=0
To view the dependency tree of a specific package, use the
npm view <YOUR_PACKAGE> dependencies
command.
npm view <YOUR_PACKAGE> dependencies
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.
npm view axios@1.4.0 dependencies
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-remote-ls
If you want to view all dependencies of a package (the whole tree), use the
npm-remote-ls
package.
npx npm-remote-ls <YOUR_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.
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.
npm-remote-ls <YOUR_PACKAGE>
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.
npm view react dependencies
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.
npx npm-remote-ls react@18.2.0
The example prints the dependencies tree of the package react
at version
18.2.0
.
howfat
There is also a howfat
module that you can use to view the dependency tree of
a specific package.
npx howfat <YOUR_PACKAGE>
You can also specify the version of the package for which you want to view the dependencies tree.
npx howfat react@18.2.0
The command prints the dependencies tree for react
version 18.2.0
.
You can view more examples of using the howfat
package on its
NPM page.
If you use yarn
, you can also use the yarn why <PACKAGE>
command to check
why a module is installed.
yarn install
command to generate a yarn.lock
file if you haven't got one.yarn install
Now issue the yarn why <YOUR_PACKAGE>
command.
yarn why <YOUR_PACKAGE>
As shown in the screenshot, the specified package exists because axios
depends
on it.
Here is another example.
The screenshot shows that the axios
package exists because it is specified in
the dependencies
object of the package.json
file.
You can learn more about the related topics by checking out the following tutorials: