Find all node_modules and their sizes

If you’re a node developer, you’ll likely accrue a lot of node_modules directories across your system. These can take up gigabytes of disk space.

You can use find to locate these, but I’ve since discovered the alternative fd tool which is faster.

brew install fd

You can then run:

fd node_modules ~/Documents --prune -Ix du -sh | sort -rh
Command / flag Description
fd Faster find by running multiple threads.
node_modules The name of the file or directory to find.
~/Documents The root location to search in.
--prune Once a hit is found, do not traverse into child directories.
-I Do not obey .gitIgnore files.
-x Execute the next command for each hit as it’s found.
du Disk usage tool, for calculating size of directory on disk.
-s Display an entry for the specified file/directory.
-h Use human-readable sizes.
sort Sort tool.
-r Reverse the sort, so largest are at the top.
-h Sort by numerical value, but take into account the disk size suffix, if present.

Tested on macOS.