How to Delete ALL node_modules folders on your machine .

Mark Pieszak | Trilon Consulting
Mark Pieszak

Whenever we work on a new project we need to run npm install, but how often do we think about the toll this takes on our hard-drive?

The dreaded "out of space" message

A single node_modules folder can take up anywhere from 200+ megabytes of space (sometimes 1GB+ !).

Now take a look at your github/ folder, or wherever else you're storing the majority of your projects, and you can start to see where some of your free hard-drive space has gone!

I recently tweeted about this, as it's something I use every few months and wanted to share with others who might have ran into the same situation themselves!

List all node_modules found in a Directory:

First, let's take a look at ALL the node_modules we have in a directory, before we actually start deleting them!

NOTE: Make sure you cd into a specific directory where most of your projects are. ie: documents or documents/github.

Mac / Linux:

This command will print out each folder, and even show us how much space the folder is occupying !

$ cd documents
$ find . -name "node_modules" -type d -prune -print | xargs du -chs

--- Example output ---
1.0G ./Github/big-project/node_modules
225M ./Github/
482M ./Github/Some_Demo/node_modules

1.7G total


$ cd documents
$ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

This list will give you a good idea of just the sheer amount of projects you have installed on your machine!

Delete all node_modules found in a Directory:

NOTE: Use caution here, and make sure that you are in a directory where you're comfortable removing all the instances of node_modules, run the script above to see a full list of them all before deleting.

This script is actually very similar to the one above, but we're going to be utilizing rm -rf to completely delete them.

WARNING: This process is irreversible!

Mac / Linux:

$ cd documents
$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;


$ cd documents
$ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

Powershell Users:

Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force

There we have it!

For me personally, this typically clears out about 40-60GB from my hard-drive, but your mileage may vary!

In Conclusion

  • Make sure to list all node_modules in a given directory BEFORE deleting them.
  • Make sure to be cautious as this process is irreversible!
  • Remember to npm install on the projects you need to work on again.
  • Enjoy the free space! :)

Learn NestJS - Official NestJS Courses 📚

Level-up your NestJS and Node.js ecosystem skills in these incremental workshop-style courses, from the NestJS Creator himself, and help support the NestJS framework! 🐈

🚀 The NestJS Fundamentals Course is now LIVE and 25% off for a limited time!

🎉 NEW - NestJS Course Extensions now live!

Share this Post!

📬 Trilon Newsletter

Stay up to date with all the latest Articles & News!

More from the Trilon Blog .

Thiago Martins | Trilon Consulting
Thiago Martins

Test Driven Development with NestJS - Integration Tests

In this 2nd part of the series on Test Driven Development. Let's learn how use TDD in our Integration tests in NestJS.

Read More
Thiago Martins | Trilon Consulting
Thiago Martins

Applying Test Driven Development with NestJS

TDD is a powerful discipline that allows us to create reliable applications using tests to drive its implementation. Let's learn how to do it using NestJS.

Read More
Jay McDoniel | Trilon Consulting
Jay McDoniel

Circular Dependencies in NestJS and how to Avoid Them

A deep dive on Circular Dependencies with NestJS and tips for how to find and avoid them

Read More

What we do at Trilon .

At Trilon, our goal is to help elevate teams - giving them the push they need to truly succeed in today's ever-changing tech world.

Trilon - Consulting

Consulting .

Let us help take your Application to the next level - planning the next big steps, reviewing architecture, and brainstorming with the team to ensure you achieve your most ambitious goals!

Trilon - Development and Team Augmentation

Development .

Trilon can become part of your development process, making sure that you're building enterprise-grade, scalable applications with best-practices in mind, all while getting things done better and faster!

Trilon - Workshops on NestJS, Node, and other modern JavaScript topics

Workshops .

Have a Trilon team member come to YOU! Get your team up to speed with guided workshops on a huge variety of topics. Modern NodeJS (or NestJS) development, JavaScript frameworks, Reactive Programming, or anything in between! We've got you covered.

Trilon - Open-source contributors

Open-source .

We love open-source because we love giving back to the community! We help maintain & contribute to some of the largest open-source projects, and hope to always share our knowledge with the world!

Explore more

Write us a message .

Let's talk about how Trilon can help your next project get to the next level.

Rather send us an email? Write to:
© 2019-2022 Trilon.