Every developer who has worked with Node.js projects knows the drill: run npm install to get dependencies, and before long, your disk space mysteriously vanishes. Those node_modules directories accumulate across projects you haven't touched in months, or maybe years. A single project can contain anywhere from 50MB to over 200MB of dependencies, and when you multiply that across dozens of projects, you're looking at gigabytes of wasted space.
This guide covers how to safely find and delete all those node_modules directories from your computer, along with best practices for keeping your development environment lean and efficient. Our web development services team regularly helps clients optimize their development workflows and infrastructure.
Why Node Modules Directories Consume So Much Space
The Nature of npm Dependencies
When you run npm install in a Node.js project, the package manager downloads every dependency and its sub-dependencies into the node_modules folder. Modern web development with frameworks like Next.js, React, and Vue often requires dozens of direct dependencies, each of which brings along its own set of transitive dependencies. A single package like lodash might pull in additional packages, and those packages pull in more, creating a deep dependency tree that multiplies quickly.
The average medium-sized project can easily contain hundreds of megabytes in its node_modules directory. Large enterprise applications or projects using heavy libraries can exceed 1GB or more. Over time, as you work on different projects, create test applications, or clone repositories you never complete, these directories accumulate across your filesystem without you even noticing.
Projects You May Have Forgotten
Many developers maintain multiple projects simultaneously or return to older codebases periodically. Some projects become abandoned, others are one-off experiments, and still others represent work you simply don't do anymore. Yet the node_modules folders for all these projects remain on your disk, silently consuming space you could use for current work, media files, or system resources.
The solution isn't to stop using npm--dependencies are essential for modern web development--but to periodically clean up the directories you no longer need. This keeps your development environment organized and ensures you're not wasting disk space on projects that have served their purpose.
Testing Before Deletion
Preview What Will Be Removed
Before running any destructive commands, you should always test to see exactly what the command will find and how much disk space you'll recover. This prevents accidental deletion of projects you actually need and gives you confidence in the cleanup process.
Mac/Linux Command:
cd ~/Projects
find . -name "node_modules" -type d -prune -print | xargs du -chs
Windows Command:
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"
This command searches for directories named node_modules, prunes the search to avoid descending into them (which would count files multiple times), and then calculates the total disk usage. The output shows each directory's size individually and provides a grand total at the end.
Interpreting the Results
Review the output carefully. Look for any projects you might still need. If you find node_modules directories for active projects that you simply haven't used recently, you can keep those--the dependencies will be reinstalled when you run npm install again. Focus on deleting directories for projects you truly no longer need, old experiments, or abandoned work.
Note the total disk space usage. Many developers are surprised to discover they've accumulated 10GB, 20GB, or more in orphaned node_modules folders.
Deleting Node Modules Directories
Mac and Linux Commands
Once you've reviewed the test results and are satisfied with what will be deleted:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
For a slightly faster version that uses xargs to batch the deletions:
find . -name 'node_modules' -type d -prune -print | xargs rm -rf
Windows Commands
Command Prompt:
FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"
PowerShell:
Get-ChildItem -Recurse -Directory -Filter "node_modules" | Remove-Item -Recurse -Force
Git Bash:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
Running From the Correct Directory
The key to safe deletion is running these commands from the right starting directory. Navigate to your parent code directory first--where all your projects live--not into individual project folders. This ensures the command finds and deletes all node_modules across all your projects in one pass.
More sophisticated options for visual and selective cleanup
npkill
Visual command-line tool that displays node_modules directories sorted by size, allowing interactive deletion with keyboard navigation.
Rimraf
Robust deletion package that handles permission errors, locked files, and long paths more gracefully than standard deletion.
VS Code Extensions
Integrated extensions that provide visual interfaces for managing node_modules directly within your development environment.
1# Install npkill globally2npm install -g npkill3 4# Run from your projects directory5npkill6 7# The interface shows each node_modules with its size8# Use arrow keys to navigate, Space/Enter to deleteClearing the npm Cache
When and Why to Clear Cache
After deleting node_modules directories, you may also want to clear npm's cache. npm stores downloaded packages in a cache directory to speed up future installs. Over time, this cache can grow significantly, and it may contain corrupted or outdated package data that causes installation issues.
Cache Commands
Clear npm cache:
npm cache clean --force
Verify cache status:
npm cache verify
Yarn equivalent:
yarn cache clean
For more details on npm cache management, refer to PhoenixNAP's npm cache guide which provides comprehensive documentation on cache best practices.
Best Practices for Ongoing Management
Selective Deletion Strategy
Rather than deleting all node_modules folders indiscriminately, adopt a selective approach. Delete node_modules for projects you no longer actively maintain or work on. Keep node_modules for current projects, even if you haven't worked on them recently--you never know when you'll need to make a quick fix or review.
A good rule of thumb: if you haven't opened a project in the last three months and don't have immediate plans to do so, it's a candidate for node_modules deletion.
Use Package Lock Files
Always ensure your projects have package-lock.json or yarn.lock committed to version control. These lock files guarantee that when you reinstall dependencies after deletion, you get exactly the same versions that the project was developed with.
Consider pnpm
If disk space is a persistent concern, consider using pnpm (Performant npm) which uses a content-addressable storage system and hard links to share packages across projects. This can reduce the total disk space used by dependencies significantly. Our web development team often recommends pnpm for projects with multiple team members working on similar tech stacks. Additionally, AI automation services can help streamline development workflows by automating repetitive setup and maintenance tasks across your project portfolio.
1# Standard install (uses package.json and package-lock.json)2npm install3 4# Clean install for CI environments (faster, more reliable)5npm ci6 7# Verify installation8npm run buildFrequently Asked Questions
Summary
Deleting node_modules directories is a safe and effective way to reclaim disk space on your development machine. Start by testing with preview commands to see what will be deleted and how much space you'll recover. Use platform-specific deletion commands or tools like npkill for visual management. After deletion, clear the npm cache for a fresh start, and adopt ongoing practices like periodic cleanup and selective deletion to prevent future accumulation.
Remember that node_modules can always be reinstalled from package.json and package-lock.json, so don't be afraid to delete folders for projects you no longer need. Your future self will thank you when your development environment is lean, fast, and free of digital clutter.