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!
<script>#npm #protip ✍️
— Mark Pieszak (@MarkPieszak) August 7, 2019
Free up space on your HD by recursively deleting ALL /node_modules/ within a given directory.
🤯 Free'd up 60GB personally pic.twitter.com/FSnMjpLctO
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!
NOTE: Make sure you cd into a specific directory where most of your projects are. ie:
documents
ordocuments/github
.
Mac / Linux:
This command will print out each folder, and even show us how much space the folder is occupying !
$ cd documents$ find . -name "node_modules" -type d -prune -print | xargs du -chs--- Example output ---1.0G ./Github/big-project/node_modules225M ./Github/Trilon.io/node_modules482M ./Github/Some_Demo/node_modules1.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:
NOTE: Use caution here, and make sure that you are in a directory where you're comfortable removing all the instances of node_modules, run the script above to see a full list of them all before deleting.
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
There we have it!
For me personally, this typically clears out about 40-60GB from my hard-drive, but your mileage may vary!
In Conclusion
- Make sure to list all nodemodules in a given directory _BEFORE deleting them.
- Make sure to be cautious as this process is irreversible!
- Remember to
npm install
on the projects you need to work on again. - Enjoy the free space! :)
Learn NestJS - Official NestJS Courses 📚
Level-up your NestJS and Node.js ecosystem skills in these incremental workshop-style courses, from the NestJS Creator himself, and help support the NestJS framework! 🐈🚀 The NestJS Fundamentals Course is now LIVE and 25% off for a limited time!
🎉 NEW - NestJS Course Extensions now live!
- NestJS Advanced Concepts Course now LIVE!
- NestJS Authentication / Authorization Course now LIVE!
- NestJS GraphQL Course (code-first & schema-first approaches) are now LIVE!
- NestJS Authentication / Authorization Course now LIVE!