DEV Community

Cover image for Who Ate My Disk Space? It's "node_modules" Again! 🕵️‍♂️
Krisztián Maurer
Krisztián Maurer

Posted on • Updated on

Who Ate My Disk Space? It's "node_modules" Again! 🕵️‍♂️

Hey there! If you've used Node.js, you've probably seen the node_modules folder. It's where all the extra stuff for your project goes. But sometimes, it gets really big, really fast. I wanted to know just how big, so I made a script to check it out.

Maurer Krisztian

What I Made:

It's a simple tool that looks through your projects, finds the node_modules folders, and tells you how big they are. And it uses colors to be cool!

Why I Made This:

  1. Curiosity: Which project has the biggest node_modules? 🤔
  2. Cleaning: Some folders just have too much stuff. 🧹
  3. Fun: I thought it'd be a fun little project. 😄
  4. AI: Can AI craft this for me? (Absolutely, with a bit of direction!)

The script

#!/bin/bash

# ANSI color codes
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
RESET="\033[0m"

# Set the starting directory for the search. Default is root if no argument is provided.
START_DIR=${1:-/}

# Find top-level node_modules directories directly under a project within the specified directory
# and exclude those inside the .cache directory
directories=$(find "$START_DIR" -type d -name "node_modules" -not -path "*/node_modules/*/*" -not -path "*/.cache/*" 2>/dev/null)

total_size=0

# Loop through each directory and calculate its size
for dir in $directories; do
    # Check if the parent directory of node_modules is not another node_modules (to avoid nested ones)
    parent_dir=$(dirname "$dir")
    if [[ $(basename "$parent_dir") != "node_modules" ]]; then
        size=$(du -sh "$dir" | cut -f1)
        echo -e "${YELLOW}Size of $dir: ${GREEN}$size${RESET}"

        # Convert size to bytes for summation
        bytes=$(du -sb "$dir" | cut -f1)
        total_size=$((total_size + bytes))
    fi
done

# Convert total size back to human-readable format
human_readable_total=$(numfmt --to=iec-i --suffix=B $total_size)

echo -e "${RED}Total size of all top-level node_modules within $START_DIR: ${GREEN}$human_readable_total${RESET}"

Enter fullscreen mode Exit fullscreen mode

*gh gist

Want to Try? Here's How:

  1. Write down the script in node_modules_size.sh.
  2. Get it ready with: chmod +x node_modules_size.sh.
  3. Start it up: ./node_modules_size.sh.
  4. For a specific place: ./node_modules_size.sh /path/to/place.

Maurer Krisztian

Wrap Up

So, if you're wondering where all your computer's space went, this tool might help. Give it a go and see what you find! 🚀📁

Top comments (6)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

Seeing your issue I'd suggest to switch to pnpm, as it tries and hard link dependencies in your disk across multiple projects, this way if you have, let's say, 40 react projects you'll have it installed just once and the rest get hard linked behind the scenes 😋

Great bash script and post header image though! 😁

Collapse
 
maurerkrisztian profile image
Krisztián Maurer • Edited

Hey! Yes, pnpm will probably reduce my 26GB node_modules.. I was hesitant to switch because of the npm package lock, but now I discovered I can easily migrate using pnpm import.

Thanks for the kind words about the script and the pic. Really appreciate it! 😁

Collapse
 
wraith profile image
Jake Lundberg

came to say this same thing, but ya beet me to it! 😂

Collapse
 
utteshkumar profile image
uttesh

Good script! I've been in the same situation with space issues caused by 'node_modules'; it devours space like a monster. A couple of years ago, I added a similar npm CLI that provides all project details, including space usage its called mo.

Collapse
 
wraith profile image
Jake Lundberg

Great image choices here 🤣 Love it!

Collapse
 
jamesta696 profile image
Jamie Smith

Nice article!
That's a perfect example of ReactJS lol 😂
Something so simple yet a framework or library needs a ton of node_modules just to function properly.