node_modules
are the closet of your developer life. And just like that closet, it's packed to the brim with things you might never use again. Ever wondered how much digital real estate they're hogging?
Let's find out.
Visualizing the clutter
Pro Tip: Make sure you're in the right neighborhood.
cd
into the directory where most of your projects live, likedocuments
ordocuments/github
.
For the Mac and Linux folks
Want to know how much space you're dealing with? This command has got you covered:
cd documents
find . -name "node_modules" -type d -prune | xargs du -chs
Example output
255M ./Github/Trilon.io/node_modules
482M ./Github/Some_Demo/node_modules
707M total
If you like Nodejs, see our GitHub
If you're on Windows
Run this baby right here:
cd documents
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"
This will show you the plethora of node_modules
you’ve been collecting. A digital hoarder’s dream!
Join the conversation about open source in React, explore WebCrumbs
Time to take out the trash
Heads up: Don't just pull the trigger. Double-check your aim by running the above scripts to know exactly what you're about to delete.
For the Mac and Linux crew
Execute this to wipe node_modules
off the face of your drive:
cd documents
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
Want to be a part of our community? Dive into code
And for the Windows warriors
cd documents
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"
Join our open-source movement and become a maker
The afterparty
I usually free up about 40-60GB doing this. Your results may vary, but hey, free space is free space!
Wrapping it up
- Check before you wreck — list all
node_modules
first. - Proceed with caution; this operation's got no Ctrl-Z.
- Remember to run
npm install
on projects you'll be revisiting.
And there you have it. You just decluttered your digital world.
Follow me for more tips.
Top comments (6)
Isn't this what
npm prune
does? Remove unused modules? Or instead, use yarn:classic.yarnpkg.com/lang/en/docs/c...
No need to blast everything and then install everything again. That's only helping kill your SSD faster.
Yes.. in my case, there were so many places with node_modules folders that other alternatives would get me crazy trying to figure them out! It was that bad...
But it seems like you could use your script but instead of deleting, you could run npm prune. BUt of course, if these are older projects that you don't use often, go ahead and delete the whole thing!
I recommend
npx npkill
. You can select from which directoriesnode_modules
should be removed and gives you amount of space you've saved.if u are a student or work in Corporate we are stuck with Node.js. This is just the way it is. Two ways out...#1 use AWS Lambda where u can import one or 2 Node modules or #2..use plain vanilla javascript with no Node at all and just Fetch everything. When I was in Corporate I had the luxury of doing many ad-hoc programs for small use cases like dashboards. I ditched all the old paradigms like Node and Java and I even used SQLite for my data and no one even knew. Boss was not smart enough as he only wanted the deliverable which was that the project code worked. Then I even ditched SQLite and started storing my data as a JSON string in a document db in AWS S3 and still no one knew. So, since Fetch API came out in 2016 this revolutionized plain javascript.
Yes. I needed this quick fix to install the new iOS. It’s still installing and I’m here waiting… 🥲