DEV Community

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

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

Leonardo Montini on August 05, 2022

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 proj...
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 Playful Programming •

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
 
zaidmaker profile image
DevMirza •

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

Collapse
 
mjcoder profile image
Mohammad Javed •

rm -rf it all 😆

Just kidding.

Keep the informative articles coming, thank you.

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 Playful Programming •

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 Playful Programming •

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