How to delete all node_modules directories from your computer

og.png
How to delete all node_modules directories from your computer 1

How to delete all node_modules directories from your computer | Go Make Things

Over the last few years, I’ve run npm install more times than I can count. And as a result, I have a lot of node_modules directories on my computer that I long forgot about and never use.

Sometimes I don’t need the project at all anymore, but often, it’s a codebase I just don’t work with often.

My node_modules directories contained 50mb of stuff on the small side, and over 200mb of files in some cases. Over a few dozen projects, that really adds up!

Yesterday, I decided to delete every single node_modules directory off of my computer, and selectively reinstall the ones I need only when I need them.

That could be a really long, arduous manual task. But fortunately for me, Mark Pieszak wrote an article about how to do it with a single command line script.

Testing first

Before doing this, it’s a good idea to test the script and make sure it’s not going to do anything you don’t want it to.

First, in a terminal/CLI window, cd into whatever directory contains most of your code projects. On my Mac, that’s sites. Your setup might be different.

cd ~/sites

Then, run this code.

# Mac/Linux
find . -name "node_modules" -type d -prune -print | xargs du -chs

# Windows
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

This will spit out a list of all of the node_modules that it finds, and how much disk space they’re taking up (individually and in total).

Actually deleting files

If you’re happy with what the script finds, you can now delete all of the directories off of your computer.

# Mac/Linux
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' ;

# Windows
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

And that’s that. Thanks to Mark and the Trilon folks for documenting this!


Made with ❤️ in Massachusetts. Unless otherwise noted, all code is free to use under the MIT License. I also very irregularly share non-coding thoughts.


This post was originally posted here

Check out our Starter Sites built with #ToolWeLove including Toolset, Elementor Pro, and Astra Pro.

 

Share this page
Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on email
Pixallus Website Giveaway

We understand COVID has created shifts in many people’s lives. That’s why we’ve decided to giveaway a fully functional website with all the tools you

Read More »
How to Choose WordPress Hosting

Pixallus WordPress Hosting. Simplified. Enjoy blazing fast WordPress website loading speeds and 99.9% uptime. Whether you’re a freelancer, small business, or a large organization, we

Read More »
Top Pizza Franchises for 2020

Pizza is always in demand with consumers, which means it provides plenty of thriving franchises. Today’s best pizza franchises range from traditional delivery outlets to

Read More »
Warp SVG Online

This post was originally posted here Check out our Starter Sites built with #ToolWeLove including Toolset, Elementor Pro, and Astra Pro.  

Read More »
sendinblue