DEV Community

Cover image for Running out of disk space? Delete node_modules
OpenSource for WebCrumbs

Posted on • Updated on

Running out of disk space? Delete node_modules

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

Clutter


Pro Tip: Make sure you're in the right neighborhood. cd into the directory where most of your projects live, like documents or documents/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
Enter fullscreen mode Exit fullscreen mode

Example output

255M ./Github/Trilon.io/node_modules
482M ./Github/Some_Demo/node_modules 
707M total
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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 '{}' +
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

  1. Check before you wreck — list all node_modules first.
  2. Proceed with caution; this operation's got no Ctrl-Z.
  3. Remember to run npm install on projects you'll be revisiting.

Take a Bow

And there you have it. You just decluttered your digital world.

Follow me for more tips.


WebCrumbs

Check it out at webcrumbs.org

Star our GitHub if you haven't yet ;)

Top comments (6)

Collapse
 
lopis profile image
Joao L.

Isn't this what npm prune does? Remove unused modules? Or instead, use yarn:

classic.yarnpkg.com/lang/en/docs/c...

The prune command isn’t necessary. yarn install will prune extraneous packages.

No need to blast everything and then install everything again. That's only helping kill your SSD faster.

Collapse
 
opensourcee profile image
OpenSource • Edited

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...

Collapse
 
lopis profile image
Joao L.

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!

Collapse
 
bartoszkrawczyk2 profile image
Bartosz Krawczyk

I recommend npx npkill. You can select from which directories node_modules should be removed and gives you amount of space you've saved.

Collapse
 
rickdelpo1 profile image
Rick Delpo

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.

Collapse
 
opensourcee profile image
OpenSource

Yes. I needed this quick fix to install the new iOS. It’s still installing and I’m here waiting… 🥲