DEV Community

Cover image for npkill - The solution to deleting node_modules easily, with style
Carlos Caballero
Carlos Caballero

Posted on • Updated on • Originally published at carloscaballero.io

npkill - The solution to deleting node_modules easily, with style

Introduction

One of my happiest memories related to software development was when I developed and distributed my first project among my friends and family. Although I learned to develop long before then, it was in 2003 when I developed the classic game Arkanoid in assembler. Of course, this game couldn't be distributed online, since we had no internet at home, GitHub didn't exist, and, in my particular case, I didn't know any social networks for developers. Today, this story is a beautiful memory stored in my mind... And possibly in old floppies, kept in old boxes somewhere in my parent's house.

Nowadays, I teach developers and I'm constantly encouraging my students to give visibility to all their projects, and to rely on the community, since it is there where they will find the best colleagues, outside the classroom. The story I previously told would have gone very differently if it had happened today, since the first thing I would have done is to publish my project on all social networks, and find a good mentor to learn from her/him.

Although you can't go back in time, I am pleased to present a project developed by two of my former vocational training students, which is published on GitHub. I think it is a great contribution to the community.

This tool solves a traditional problem that us node.js developers have when we install a large set of dependencies in our projects. The number of node_modules folders in our computer grows as we develop more projects.
More often than not, we don't remember where the node_modules folders are stored, so it is very difficult to find and delete them.

node_modules_meme

These two students (now developers) have developed an npm package which allows management of our old and heavy node_modules folders. Therefore, I'm very happy to announce npkill.

npkill

npkill

This tool allows you to list any node_modules directories in your system, as well as the space they take up. You can then select which ones you want to erase to free up space. Yay!

Before to explain the easiest steps to get this essential tool, I'm going to show a GIF in which I've been using this tool in my laptop to save more than 1 GiB over a work folder.

ezgif.com-optimize

Installation

$ npm i -g npkill

Usage

npkill

By default, npkill will scan for node_modules starting at the path where npkill command is executed.

Move between the listed folders with , and use Del to delete the selected folder.

To exit, Ctrl + c.

Options

options |

Warning: In future versions some commands may change

Examples

  • Search node_modules directories in your projects directory:
npkill -d ~/projects

# other alternative:
cd ~/projects
npkill
  • Automatically delete all node_modules that have sneaked into your backups:
npkill -d ~/backups/ --delete-all

Conclusions

In this post, I've shown you two important topics:

  1. This amazing tool which will be very helpful to manage our node_modules.
  2. You are also able to develop an awesome project. A few months ago they were in class discovering fundamental concepts like "promises", "hoisting", "design patterns" or "tooling". Therefore, remember that you are able to do anything too.

My recommendation is to visit the npkill official repo, read the code and start helping them in this fun adventure of creating open-source content.

More, More and More...


Originally published at https://carloscaballero.io.

Discussion (35)

Collapse
morbec profile image
Fabio Morbec

Awesome! Great work guys!

Unfortunately the option --delete--all doesn't work, at least for me. I tried different shells (fish shell, zsh and bash) plus iTerm 2 and the default Terminal on macOS (10.14.6). But the individual select worked like a charm!

Collapse
nyagarcia profile image
Nya

Just published a patch. --delete-all should be fixed and working properly. If you encounter any more issues, don't hesitate to let me know and I will do my best to fix them. Thank you for your comment and your patience :D

Collapse
morbec profile image
Fabio Morbec

wow! That was fast! I even forked the project to take a look if I could help but I didn't take a look on it yet.
It working as expected now! 👍🏻

Collapse
nyagarcia profile image
Nya

Oh no... will have to work on it. Thanks for letting us know 😊

Collapse
carlillo profile image
Carlos Caballero Author

Thanks!

The guys are working in improve the project. They are very happy cause that the tool is useful to the community (in my Linux works perfectly 😂)

Collapse
sarbazx profile image
Taskhyn Maksim

used just now but didn't delet anything

Collapse
juaniman99 profile image
juaniman99

Hi taskhyn!

We believe it may be related to directory permissions. We will release a patch shortly that will notify the user about any errors related to permission problems.

Sorry for the inconvenience and thanks for trying npkill.

Collapse
carlillo profile image
Carlos Caballero Author

Hi @sarbazx !

What were your steps to run the tool?

Thanks!

Collapse
sarbazx profile image
Taskhyn Maksim

started by invoking command and then according to instruction pressed delete button or is it different key on mac?

Thread Thread
notpluto_ profile image
Srijan

Hi @sarbazx ! I am having trouble with the same. Did you managed to figure it out?

Thread Thread
sarbazx profile image
Taskhyn Maksim

Hi, no i was just testing it.

Thread Thread
nyagarcia profile image
Nya

Hey guys!

Just figured out I made a stupid mistake. Apparently the del keycode is different in mac, which is why it doesn't work (key is undefined). Will fix it right now!! So sorry!!

Thread Thread
carlillo profile image
Carlos Caballero Author

Hey @Nya!

Quiet and sure the patch is on its way :-).

Thank you very much for your app.

My MacBook wants to reduce the weight of the node_modules.

Cheer up!

Thanks!

Collapse
nyagarcia profile image
Nya

As you probably know already, using find/exec will delete every single result the command yields.
Npkill allows you to select which folders you want to erase, as well as calculating the amount of freed space... Without having to remember any lengthy commands :D

Collapse
harken24 profile image
Jozo • Edited

It's not good practice to install NPM packages with sudo, I'd rather use npx command.
Of course correct me if I'm wrong.
But good job for developers it's a package I'll use definitely.

Collapse
carlillo profile image
Carlos Caballero Author

Totally true!

Collapse
th3n00bc0d3r profile image
Muhammad

Astonishing.. I'd say just one word...

Collapse
carlillo profile image
Carlos Caballero Author

Hi @th3n00brc0d3r!

They are very happy for their first open source project.

Thanks.

Collapse
webdevinci profile image
webdevinci

Well this helps a ton of things. Great share

Collapse
carlillo profile image
Carlos Caballero Author

Hi @webdevinci !

How many tons of gigabytes have you freed up?😂😂

Thanks!

Collapse
webdevinci profile image
webdevinci

I have 6.2 GB there (not too bad, not too good). I figure i can get rid of a 1/3 at least! It's a beautiful thing

Thread Thread
carlillo profile image
Carlos Caballero Author

Hi!

Really nice!

☺️

Collapse
pabloc profile image
PabloC

On Mac just:

find . -name "node_modules" -exec rm -rf '{}' +'

would do the same I think.

Collapse
duckbrain profile image
Jonathan Duck

Check out ncdu.

Collapse
carlillo profile image
Carlos Caballero Author

Hi!

Thanks!

Collapse
hmphu profile image
Hoang Manh Phu

Awesome Jobs Guy!!!

Collapse
codemouse92 profile image
Jason C. McDonald

Love the illustration, btw. ;)

Collapse
carlillo profile image
Carlos Caballero Author

Hi @codemouse92 ,

The logo has been created by them too.

Thanks!

Collapse
valeriopisapia profile image
Valerio

Thanks a lot, Mate :)

Collapse
justingivens profile image
Justin D. Givens

AWESOME tool!! Didn't realized I wasted over 10G of space on node_modules!

Feature suggestion: Row highlighting or something like that.

Collapse
nyagarcia profile image
Nya

Glad you found it useful! Great suggestion, will add it to the roadmap, thanks ;D

Collapse
omardulaimi profile image
Omar Dulaimi

This is really awesome, I still remember when I used to manually delete each node modules folder one by one. Great addition to the npm community indeed.

Collapse
lc_fd profile image
Luca Fedrizzi

Cool idea! Thank you ❤️