A quick way to find out how much Disk Space your node_modules take up (on Mac Linux or Windows)

Learn how to find all your node_modules directories, how much disk space they take up, and how to remove them — fast.

If you’re like most developers, you’ve run npm install or yarn a lot of times. As a result, you probably have a ton of node_modules directories from older projects that you never use. These node_modules serve you no purpose except for bloating your hard drive and making your computer slower.

Here’s how to find out exactly how many node_modules directories you have installed on your computer (Mac, Linux, or Windows) and how much space they take up individually and in total.

Prepare yourself for a shock.

Find all node_modules

Open up your Terminal/Command line, and cd into the directory where you store your code projects and run this code:

# Mac or Linux
find . -name "node_modules" -type d -prune -print | xargs du -chs

# Windows
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

This could take a few seconds, depending on how much crap (I mean amazing projects) you have on your computer.

When it’s done, your Terminal will spit out a list overview of every node_modules directory you have installed. It will also show their location, how much space they take up individually, and the total amount of space they take up:

Mac terminal displaying list of node_modules
A whopping 13GB and this is after I cleaned up my hard drive recently 🤯

Delete all node_modules

If you want to delete all your node_modules directories, run this command:

# Mac or Linux
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

# Windows
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

Wait, isn’t it risky to just wipe out all node_modules directories?

No, because you can always install them again. Chances are that you’ll realize that you’re only using 10% of them, the rest are just lying around doing nothing but bloating up your computer.

Credit Mark Pieszak who wrote about this on his blog.

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊


Share & Discuss on