DEV Community

Cover image for ♻️ Delete unused node_modules in a second and enjoy some free space!
Leonardo Montini for This is Learning

Posted on • Updated on • Originally published at leonardomontini.dev

♻️ Delete unused node_modules in a second and enjoy some free space!

Intro

In this article I'll tell you about a cool tool I just found on npm, called npkill, that helps you cleaning node_modules from projects you no longer user but still want to keep.

YouTube Video

Video link: https://youtube.com/shorts/76UXyiPJQwI

Table of Contents

The problem

This tool is mostly for active developers who have many projects in their computer, but everyone can get benefit from it.

Everytime you run npm install or yarn on a new repository, you're basically throwing a lot of files inside the local node_modules folder.

As time goes by, you'll end by having a lot of those folders on old and dusty projects sitting there, wasting space in your disk.

For many reasons, you might want to keep those projects and not delete them, for example because of some particular implementations you want to keep as examples or template, and that's totally fine, but you don't need the node_modules folder!

The solution

I found a cool tool that helps exactly for this scenario, it's called npkill and you can find it on npm.js.

npkill

This CLI tool will navigate for your disk looking for node_modules folders and will list all of them. After the scan, you will have the power to select them one by one and hit SPACE to delete the entire folder.

Installation

As it's a regular package on npm, it's as easy as



npm i -g npkill


Enter fullscreen mode Exit fullscreen mode

Once you've installed in globally, you can run



npx npkill


Enter fullscreen mode Exit fullscreen mode

and the hunt begins!

Options

The tool comes with some options you can find in the README on GitHub, but I'd like to put the spotlight in some of them that are quite useful

  • --sort or -s => Allows to sort by size or path. I think sorting by size is super handy!
  • --target or -t => Allows to set a different target folder than node_modules. You can use this option to clean up dist or build folders for example.
  • --directory or -d => Allows to specify the folder to look at. By default is the current path, but you can run it from everywhere and set a different path without repositioning your terminal.

Contributing

The repository is public and hosted in GitHub, but does not seem too active.

This doesn't mean the project is dead though, maybe they're still open for external help.

You can find the repo here: https://github.com/voidcosmos/npkill/issues

Conclusions

And that was it! I found this really handy tool by browsing the internet and cleaned up approximately 8GB on my computer, I was so happy that I wanted to share it with you!

Let me know if you already knew this tool or if you're going to use it... how much space did it save you?


Want to see it in action? Check out this YouTube #Shorts video!
YouTube Video


Thanks for reading this post, I hope you find it interesting!
Feel free to follow me to get notified when new articles are out ;)



You can also follow me on Twitter and YouTube!

Twitter YouTube

Top comments (14)

Collapse
 
rmariuzzo profile image
Rubens Mariuzzo

From time to time, I usually run a script like:

find . -type d \
  -ctime +90 \
  -name node_modules \
  -depth 2 \
  -exec du \
  -hs {} \;
Enter fullscreen mode Exit fullscreen mode

It displays untouched node_modules from the last 90 days. After reviewing, I activate the flag to delete them.

Collapse
 
balastrong profile image
Leonardo Montini

That's a good one!

If I remember correctly npkill was also planning to add a min date feature to filter old node modules, but isn't implemented yet.

Collapse
 
zabdeldjallil profile image
Djilou

Yes indeed, but pnpm doesn't work well with older versions of node (v10 for example:yes some companies still use it) so this package can be a life saver for servers/personal pc disk space

Collapse
 
sirseanofloxley profile image
Sean Allin Newell

If you are reading this, and work at such a company, that is a huge risk. I strongly recommend to move to node 14 and then node 16 layer this year as LTS support for v12 is over. It's a scary security posture otherwise! (And the side bonus you can explore server side esmodules, pnpm, vite... Etc 👍🔥)

Collapse
 
mjcoder profile image
Mohammad Javed

rm -rf it all 😆

Just kidding.

Keep the informative articles coming, thank you.

Collapse
 
zaidmaker profile image
DevMirza

I am able to release freaking 9 GB from my disk 🤯

Collapse
 
andrewbaisden profile image
Andrew Baisden

Useful tip!

Collapse
 
selectivehouse profile image
Joe Jordan

This is great thanks very much, i've got some chonky folders locally that are in need of a cleanup!

Collapse
 
dillonb07 profile image
Dillon Barnes

Wow, thanks! That's really useful. I was planning to do this manually, but this saved me A LOT of time and 9.88GB of space!

Collapse
 
balastrong profile image
Leonardo Montini

That's great to hear, thank you for sharing this!

Collapse
 
moopet profile image
Ben Sinclair

If you really want to save space, you could always run the npx command without the install first... or maybe find . -type d -name node_modules | xargs du -sh

Collapse
 
skylersaville profile image
Skyler Saville

I was wonder why you would use npx after globally installing it. Using the npx command by itself seems a much better practice.

Collapse
 
balastrong profile image
Leonardo Montini

That's indeed correct, this will save you an extra 1.9MB of space.

Not a life changer as the many GB you will free a few seconds later, but still a valid suggestion. Thank you for sharing! :)

Collapse
 
deanwronowski profile image
Dean Wronowski ▌

Great tip - much appreciated