Sunny
Sunny

Reputation: 553

How to calculate size of all node_modules installed on macOS?

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

Answers (3)

joemaller
joemaller

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):

  1. 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.

  2. We only want the first node_modules, so an inverse grep grep -v node_modules.*node_modules strips out every matched path containing more than one node_modules. (removed in favor of find's faster -prune flag.)

  3. 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.

  4. 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

Lafif Astahdziq
Lafif Astahdziq

Reputation: 3976

I use this command find . -name "node_modules" -type d -prune -print | xargs du -chs

Upvotes: 1

Mike
Mike

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

Related Questions