Delete Unused node_modules From Your Computer Like a Pro With "npkill"

Get storage back - in seconds.

Featured on Hashnode
Listen to this article

When you execute yarn install or npm install, plenty of node_modules are installed. Your dependencies and devDependencies and their dependencies and so on. So it can quickly happen that the node_modules folder of your project becomes several hundred megabytes large. This doesn't sound too bad at first sight, after all, most of us probably have SSDs with >= 500 GB in our laptops and several hard disks in our desktop computers. And yet, excess unused blocked storage space is not an ideal situation. For this reason, I regularly go over my local repositories with npkill and remove all node_modules that are no longer used. I will show you how to do this.

The solution - npkill

npkill-demo-0.3.0.gif

Easily find and remove old and heavy node_modules folders ✨

(text and gif source)

That is exactly what npkill is all about and what it is. It looks for all node_modules (sub-)folders starting at the path where npkill was executed, suggests them for deletion, and removes selected ones when you confirm the selection. This way you can very quickly remove unnecessary clutter. Think of it like docker system prune only for node_modules.

How to use npkill

You do not need to install npkill as you can simply start it with:

npx npkill

You will see a list of node_modules after it finished the scan. Move up and down with ↓ ↑ and hit Space to delete the selected folder. To exit, hit Q or Ctrl + c. Now you can watch npkill doing its job.

There are more options available, which are listed on the npm page of npkill.

Two more things πŸ₯³

Whilst doing my research for this article, I stumbled upon some other interesting commands I want to share with you.

How to count installed packages

Do you know how many packages you have installed for your app? I didn't and was surprised about the result when I used this command.

# https://stackoverflow.com/a/57109195/1238150
npm ls | sed '/deduped$/d' | wc -l

If you want to know more about this complex command, take a look at the explanation on explainshell.com.

How to find unused packages in your package.json

I found this tool called Depcheck that is a tool for

(...) analyzing the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json.

Similar to npkill you can run it without installing:

npx depcheck

Share πŸ’›

Do you like this article? Do you want to support me? Tell your friends on Twitter about it. Thank you!

Questions and Feedback

You got any questions or want to add your thoughts? Let me know in the comments below, please.

Let's connect

Catalin Pit's photo

I cleaned 2.13GB of trash from my laptop with this. It's awesome! Thank you! πŸ”₯

Stefan Natter's photo

You're welcome! It is sometimes crazy how much storage space we leave unused but consumed. :D

πŸ€œπŸ»πŸ€›πŸ»

Hulya Karakaya's photo

Hi, I saw your post from Catalin's youtube channel. Is it okay if I delete Cache files from Visual Studio Code and yarn? I run this command(npx npkill) and it's showing me I have 95.53 GB of releasable space. Thanks for the information πŸ‘πŸ»

Stefan Natter's photo

Hi Hulya Karakaya, I’m glad you found my article.

npx will only delete node_modules. So if it says you have ~96 GB of node_modules it can delete, then yeah, it’s safe.

Hulya Karakaya's photo

Wow, thanks. I’m so excited πŸ˜€Stefan Natter