Loading Page

Originally published on the Trilon Blog on Aug 7, 2019.

Whenever we work on a new project we need to run npm install, but how often do we think about the toll this takes on our hard-drive?


A single node_modules folder can take up anywhere from 200+ megabytes of space (sometimes 1GB+ !).

Now take a look at your github/ folder, or wherever else you're storing the majority of your projects, and you can start to see where some of your free hard-drive space has gone!

I recently tweeted about this, as it's something I use every few months and wanted to share with others who might have ran into the same situation themselves!

List all node_modules found in a Directory:

First, let's take a look at ALL the node_modules we have in a directory, before we actually start deleting them! This command will print out each folder, and even show us how much space the folder is occupying!

  • Mac/Linux:

    $ cd documents
    $ find . -name "node_modules" -type d -prune -print | xargs du -chs
    --- Example output ---
    1.0G ./Github/big-project/node_modules
    225M ./Github/Trilon.io/node_modules
    482M ./Github/Some_Demo/node_modules
    1.7G total
  • Windows

    $ cd documents
    $ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

This list will give you a good idea of just the sheer amount of projects you have installed on your machine!

Delete all node_modules found in a Directory:

This script is actually very similar to the one above, but we're going to be utilizing rm -rf to completely delete them.

WARNING: This process is irreversible!

  • Mac/Linux:

    $ cd documents
    $ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
  • Windows

    cd documents
    $ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"
  • Powershell Users:

    Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force

You Might Also Like