How could I find out how much storage all my development projects' nodeJS modules are taking up?

Maybe there is a better way, but I have all the projects within a directory. I thought maybe using find /path/to/JS/projects -name "node_modules" might be a good place to start. How would I constrain the depth (so as to not calculate node_mods for dependencies) and figure out the sizes of the directories' contents and add them up?


To limit the calculation to the top-most node_modules encountered (i.e., not calculate the size for dependencies, as you mention), you can run du and then prune:

find . -name node_modules -exec du -s {} \; -prune

This will show the size and name of all the top-most node_modules directories. To calculate the sum of all the sizes, feed it into e.g. AWK:

find . -name node_modules -exec du -s {} \; -prune |
awk '{ sum += $1 } END { print sum }'

You can feed that into numfmt to get “human-readable” values:

find . -name node_modules -exec du -s {} \; -prune |
awk '{ sum += $1 } END { print sum }' |
numfmt --to=iec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.