How to find reverse dependencies on npm package?
Asked Answered
C

4

193

I'd like to find out which packages depend on express among the installed sails/kraken/loopback/hapi/koa etc. Are there npm sub-commands or other ways to locally list all reverse dependencies on one specific npm package?

Cut answered 10/8, 2015 at 15:12 Comment(0)
H
304

Adding package name after npm ls will show you tree only with the specified package.

npm ls express
Hull answered 10/8, 2015 at 16:6 Comment(4)
Thanks. The command npm view express dependencies shows only direct dependencies, but i can't figure out how to view its complete dependencies tree.Cut
only works if you have the module installed into a node_modules folder... doesn't do you any good if you're trying to resolve a dry run.Terrell
For dev dependencies, use npm ls --dev expressWilhelmina
For scoped packages (which begin with @), using only the scope @date-io fails with EINVALIDTAGNAME error. Instead use the complete package name like npm ls @date-io/date-fnsSplit
C
79

I specifically wanted to find what package used a dependency that was breaking an initial install. This may help somebody out trying todo the same:

find ./node_modules/ -name package.json | xargs grep <the_package_name>

Cavour answered 2/7, 2019 at 19:48 Comment(4)
That was exactly what I was looking for! Thanks!Abyssal
wow, finally a nice solution! I'd also add that find ./node_modules -name package.json | xargs grep -e 'PACKAGE_NAME": "' helps get rid of other mentions of the PACKAGE_NAME and focuses on instances where it is used with a version number..Hoyden
and also good to mention is that this method can be used recursively to find what first level dependency ends up importing the specified package..Hoyden
what is the alternative command for Windows Powershell?Imitation
S
5

In case someone is using pnpm, this should help to find packages that depend on lodash for example:

pnpm list --depth 1 | grep --color -E '(^\w|\slodash)'

Submultiple answered 7/6, 2021 at 11:22 Comment(0)
P
0

npm explain since v7

This command will print the chain of dependencies causing a given package to be installed in the current project.


For a PowerShell version of @Neil Gaetano Lindberg's answer:

Get-ChildItem -Path .\node_modules\ -Filter package.json -Recurse | Select-String -Pattern "<the_package_name>"

Pangolin answered 24/4 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.