Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an npm command to list the number of dependencies each of my direct depedencies is pulling in?

I know that I can get a list of my direct dependencies via

npm ls --depth=0 --only=production

which produces something like this

[email protected] /code/my-app
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

... etc ...

Is there an easy way to get the output to look something like this?

[email protected] /code/my-app
├── [email protected] (4 dependencies)
├── [email protected] (28 dependencies)
├── [email protected] (12 dependencies)
├── [email protected] (30 dependencies)
├── [email protected] (21 dependencies)
├── [email protected] (3 dependencies)
├── [email protected] (93 dependencies)
├── [email protected] (1 dependency)
├── [email protected] (7 dependencies)
like image 390
Jonathan Bender Avatar asked Apr 02 '18 20:04

Jonathan Bender


1 Answers

I don’t think npm provides this natively. Shell script can do something similar:

for path in `npm ls --depth=0 --parseable`
do
    package=`basename path`
    echo "$package: `npm ls --parseable $package | wc -l` dependencies"
done

Hope this is “easy” enough? Note that some package could be counted multiple time, since transitive dependencies can overlap with each other. It’s also assumed that package names don’t contain spaces.

like image 111
Franklin Yu Avatar answered Oct 26 '22 00:10

Franklin Yu