Skip to main content

Command Palette

Search for a command to run...

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

Get storage back - in seconds.

Published
β€’4 min read
Delete Unused node_modules From Your Computer Like a Pro With "npkill"
S

Hey πŸ‘‹πŸ»,

I am a Software Engineer from Austria πŸ‡¦πŸ‡Ή. I am interested in, write about, and develop (open source) software solutions for and with JavaScript, TypeScript, ReactJs, and NodeJS.

You can see what I am currently up to on natterstefan.me.

In the past, I was Product Owner of RAWR, Growth Hacker at the Speedinvest Startup Studio and Online Advertising Manager. Check out my LinkedIn profile for more details.

πŸ“¬ Get in touch

πŸ“§ Sign up for my newsletter.

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

H

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 πŸ‘πŸ»

1
S

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.

1
H

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

C

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

3
S

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

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

#90DaysOfProse

Part 11 of 25

Make Writing a Habit. Write for at least 10 minutes a day for the next 90 days. Tweet your progress each day with the #90DaysOfProse hashtag. More details: https://www.90daysofprose.com/

Up next

These 6 Alfred Workflows Can Help You Increase Your Productivity

Become an Alfred Powerpack Power User!