Question: How do I quickly find node_modules on my computer/server and delete them quickly? (Npkill recommended)

Purpose:

Node.js projects or front-end projects that use NPM to install dependencies always create node_modules in the project root directory and install a series of packages in it. In general, it takes up a lot of space, and as you have more projects and more dependent modules, it gets bigger and bigger, and at some point, this can cause problems. If you have a ali cloud server, cloud disk is very small, and can not or not add cloud disk, when the disk is tight, this time if you can quickly know the size of all node_modules, and delete, is a good solution to improve efficiency.

npkill

Npkill is a Node.js package that lists each node_modules folder in the system and how much space it takes up. You can then choose to delete the specific node_modules folder.

usage

NPM global installation

$ npm -g i npkill  
Copy the code

PNPM global installation:

$PNPM -g I npkillCopy the code

The image is from the Github README of npkill. The operation process is as shown in the figure above: When npkill is searching, the upper right part will show searching, indicating that the search is going on, and when the search is completed, it will show Search Completed.

As prompted, click the space bar to delete, one at a time. For more information, please check the official address document:

Tip:

This is a good solution for NPM dependencies, but if you use PNPM, you don’t need to worry about node_modules at all, because PNPM does not install modules on every node_modules, but instead links them. Finally link to the specific directory address. For PNPM’s secure dependency principle, I recommend checking out a big guy’s article (# Deep thoughts on modern package managers — Why do I now recommend PNPM over NPM/YARN?).

Due to some historical issues with NPM, and the way it is today, it is desirable to use PNPM more often than NPM.