How to Delete ALL node_modules folders on your machine .

Mark Pieszak | Trilon Consulting
Mark Pieszak
Updated on August 12, 2019

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/Trilon.io/node_modules
482M ./Github/Some_Demo/node_modules

1.7G total

Windows:

$ 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 '{}' \;

Windows:

$ 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! :)
#NPM
#Productivity

Share this Post!

More from the Trilon Blog .

Kamil Mysliwiec | Trilon Consulting
Kamil Mysliwiec

Deploy NestJS Serverless Apps to Azure Functions

Learn about the new NestJS Schematics and how to setup & deploy NestJS Serverless apps to Azure Functions in a few minutes!

Read More
Mark Pieszak | Trilon Consulting
Mark Pieszak

Deploying NestJS Apps to Zeit Now

Learn how to deploy NestJS apps to the Cloud with Zeit Now in 2 minutes!

Read More
Kamil Mysliwiec | Trilon Consulting
Kamil Mysliwiec

Announcing NestJS 6: What’s New

Today I am thrilled to announce the official release of Nest 6: A progressive Node.js framework for building efficient and enterprise-grade, server-side applications.

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!

Let us know how we can help you!

Contact Us

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:

hello@trilon.io
© 2019 Trilon, Inc.