Who develops in node Have you ever noticed that every time we start a project and run npm install
, a multitude of dependencies are installed and most of the time we don't care about the amount of files and the space they can occupy on our disk.
a single folder node_modules it can weigh between 200mb and 1gb! Multiply that by the amount of projects you have on your computer and we have a real storage problem.
I recently needed to make a backup of my projects folder, but it was weighing something around 20gb, which made the process extremely slow, even compressing, I still needed to optimize more. So I decided to remove all node_modules folders and for my no surprise, the size has reduced to just 2gb!
Finding and Listing All Folders node_modules
First let's list all folders node_modules inside the folder we want to search recursively, it's important to take a careful look so you don't delete anything you don't want.
Mac / Linux:
$cd projects
$find. -name "node_modules" -type d -prune -print | xargs du -chs
Windows:
$cd projects
$FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"
I'm using the terminal emulator from GitBash which allows me to use the rm -rf command on Windows.
Deleting all folders node_modules
Now let's add the delete option to our command. THIS PROCESS IS IRREVERSIBLE , be sure what you are doing.
Mac / Linux:
$cd projects
$find. -name 'node_modules' -type d -prune -print -exec rm -rf '{}' ;
Windows:
$cd projects
$FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"
Windows PowerShell:
Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force