Introduction
If you are a Web Developer Or Web Designer, I can be sure that you ran at least on time (if not multiple times) npm install
or yarn install
to install node_modules dependencies and if you didn't know it cost you a lot of space (about 200 MB). Imagine You have a multiple projects need to install npm modules
on them?
So, You end up with a lot of wasted space and for nothing (after you compile your assets of course). Today we will learn together how to delete node_modules
from all projects at once and free up space. So, let's get started.
Requisite
All what you need to follow along with this article is a basic knowledge of terminal/command line if you are in MAC or Linux. Or command prompt if you are in Windows.
Why We Need To Free Up Hard Drive Space
I think the answer is obvious, sometimes we need more space in our machines, and we can't find any of it, or we can delete any important file. So this is the best way to clear up space.
Calculate How Much Space Can Free
How much space freed after deleting node_modules? You can find out by running one command
After accessing your projects folder by running cd /path/into/projects/folder
for example:
1cd /media/run/coderflex/projects
Run this command
Mac/Linux:
This command will also show how much space the folder is occupying as well
1find . -name "node_modules" -type d -prun | xargs du -chs
You will see something similar:
1--- Output ---2300M ./path/into/folder/node_modules3400M ./path/into/another/folder/node_modules4700M total
Windows:
1FOR /d /r . %d in (node_modules) DO @IF EXISTS "%d" echo "%d"
Important Note
Before remove or delete any of your node_modules make sure you are in the right directory (e.g: where projects leaves)
Free Up Space in Linux/Mac
So, you can do it with simply running:
1find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
Free Up Space in Windows
In your command prompt/PowerShell run:
1FOR /d /r . %d in (node_modules) DO @IF EXISTS "%d" rm -rf "%d"
Make Your Life Easier
For making, your life easier next time you can make an alias for those commands and just type the alias and the system will run it for you. And if you are good enough with bash
language make a script to do it or even make an automation script to free up the space with cron jobs ... There are a lot of ideas and the choice is yours.
Conclusion
In this article we learn how to free up space in your system by deleting node_modules.
Before you leave I will let you with these notes:
- Make sure to list all node_modules in a given directory before deleting them.
- Make sure you are know what are you doing, cause this steps in irreversible!
- Remember to run
npm install
oryarn install
on your project in case you need to work on it again.
That's it for today and I hope you learned something new and useful.
Make sure to follow me on Twitter and if you have any question Just ask. I'm happy to help.