Node Modules Cleanup

by
, posted

If you are into Web Development, especially into Node based stack like MERN there are fair chances you have a specific directory where you save all the projects related to development work.

One thing you can relate to is whenever you setup a new environment for development or clone a repository you rely on a command to install the node modules and the dependencies related to that project.

The well known npm i or npm run install command does the job for installing related dependencies. However, over time we forget about certain projects but don’t feel like deleting them either thinking that we might come back to them in future – without realizing the storage it costs us.

Problem

The “node_modules” directory is where all the dependencies of a Node.js project are stored, and over time, it can accumulate a large amount of data, taking up significant storage space. In case of multiple projects the size even goes upto few Giga Bytes if not more.

And traversing all the projects manually, then calculating the size of modules directory and deleting them one-by-one doesn’t sound convenient.

Solution

I recently got to know about a command that automates this and makes the cleaup a breeze:

npx npkill

Working: Simply launch your terminal and cd into the directory where you have node based dev projects saved and run npx npkill. You will get an interactive command line interface to manage the modules of all your projects.

It will take few seconds to calculate the total occupied space by the node modules and display the info in top right quadrant. Use arrow keys to select the project and then hit space key to delete the modules of that directory.

In my case the idle modules were accumulating 0.6GB of size in total and I reduced that to half with the help of npkill. 600MB is significant for just six projects. I can install the modules back anytime If I ever need to come back to them by simply running npm i.

That is all for today. If you found this post useful consider sharing it with friends & subscribe for regular posts using RSS or Telegram.

Reply via mail

Your Signature