Running out of disk space? Delete node_modules

OpenSource - Oct 30 '23 - - Dev Community

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 ;)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .