Delete unused node modules with npkill

John Komarnicki

Front-End Developer | @john_komarnicki

Last Updated: July 5th, 2022

Introduction

As a developer, it's common to have many repositories accumulate over the months on your system. Depending on the project, node_modules folders can range anywhere from 50MB+.

To keep memory available and your system running smoothly, we can remove all these unused folders to free space.

Attempting to remove all of these from your project manually is tedious and time-consuming. Luckily, we don't need to do that. Instead, we can use a lightweight npm package, npkill

Setup

Using npkill is simple. You actually don't even need to install it, you can just use the command below within your terminal or command prompt.

Within a directory that has your repositories, you'll want to run this command.

$ npx npkill

Once you run that, you should see something like the image below within the terminal or command prompt. npkill will list all node_modules directories within that directory.

Removing Directories

To remove a node_modules directory, you need to use the ↓ ↑ and then space to select and delete. When you delete a directory, it does not delete the entire repository, only that node_modules folder.

It is important to notice that some directories contain ⚠️. The icon, along with a yellow background, is a warning from npkill informing you that if you remove this directory, it could break the application.

You can see an example of this in the image below.

Conclusion

npkill is a great lightweight npm package that helps you remove old node_modules folders. Hopefully, you found this brief article helpful and can use this package to free up space on your system quickly.

Have any other best practices or topics you'd want me to cover? Feel free to reach out at john@johnkomarnicki.com.

Happy coding!