Reputation: 553
I'm looking for a simple command to run calculate size of all node_modules install globally (not the ones located in projects)
Upvotes: 6
Views: 3973
Reputation: 20566
I was curious about this too. This command should work:
find . -type d -name node_modules -prune | tr '\n' '\0' | xargs -0 du -sch
Quick explanation (updated 2021-11):
The first part is easy, find . -type d -name node_modules
finds every directory -type d
named node_modules
below the current directory .
The -prune
flag prevents descending into matched paths, so nested directories like site/node_modules/some-lib/node_modules
will be hidden.
We only want the first (removed in favor of find's faster node_modules
, so an inverse grep grep -v node_modules.*node_modules
strips out every matched path containing more than one node_modules
.-prune
flag.)
The tr '\n' '\0'
command is only here to keep the following xargs
command from choking on filenames with spaces or special characters. This replaces the normal line-returns \n
with ASCII NUL \0
characters.
Finally, xargs -0 du -sch
collects everything into an argument list and passes the result to du -sch
which reports disk usage for each entry and a cumulative total using "human-readable" output.
I found just over 6 GB of assorted node_modules
directories in my projects folder.
Upvotes: 15
Reputation: 3976
I use this command find . -name "node_modules" -type d -prune -print | xargs du -chs
Upvotes: 1
Reputation: 617
if you're using using nvm:
find ~/.nvm -type d -name 'node_modules' | xargs du -csh | grep total
You can change up the find command if your not using nvm, or you want to include projects as well. If you want to find where global node_modules are located run npm list -g and it will tell you where the packages are located.
Upvotes: 2