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 / flagDescription
fdFaster find by running multiple threads.
node_modulesThe name of the file or directory to find.
~/DocumentsThe root location to search in.
--pruneOnce a hit is found, do not traverse into child directories.
-IDo not obey .gitIgnore files.
-xExecute the next command for each hit as it’s found.
duDisk usage tool, for calculating size of directory on disk.
-sDisplay an entry for the specified file/directory.
-hUse human-readable sizes.
sortSort tool.
-rReverse the sort, so largest are at the top.
-hSort by numerical value, but take into account the disk size suffix, if present.

Tested on macOS.