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