fd -I -t d node_modules | rg -v 'node_modules/(\w|@)' | xargs du -sh | sort -hr
It's very possible that there's a tool that does this, but if so please enlighten me.
The objective is to find which of all your various projects'
node_modules directory is eating up the most disk space.
The challenge is that often you have nested
node_modules within and they shouldn't be included.
The command uses
fd which comes from
brew install fd and it's a fast alternative to the built-in
find. Definitely worth investing in if you like to live fast on the command line.
The other important command here is
rg which comes from
brew install ripgrep and is a fast alternative to built-in
grep. Sure, I think one can use
grep but that can be left as an exercise to the reader.
How it works:
fd -I -t d node_modules: Find all directories called
node_modulesbut ignore any
.gitignoredirectives in their parent directories.
rg -v 'node_modules/(\w|@)': Exclude all finds where the word
node_modules/is followed by a
xargs du -sh: For each line, run
du -shon it. That's like doing
cd some/directory && du -sh, where
dumeans "disk usage" and
-smeans total and
sort -hr: Sort by the first column as a "human numeric sort" meaning it understands that "1M" is more than "20K"
Now, if I want to free up some disk space, I can look through the list and if I recognize a project I almost never work on any more, I just send it to