Loading...

How to save disk space taken by node modules?

How to save disk space taken by node modules?

So a few months ago replaced the HDD of my laptop with an SSD. But because SSDs are pretty expensive, I decided to go for a 512 GB one. I had a 1 TB HDD before but since I don’t play games on my laptop anymore I thought it’ll be enough for my projects and other work-related things. I like to experiment a lot, and I just spin up a new React project with create-react-app every time I want to try something. I quickly realized my work drive was getting full too quickly. After searching around a bit I found out that all the node_modules folders of my projects combined were taking up more than 12 GB of space! In this article, I’ll walk you through how you can view how much space your node_modules folders are taking up, and how you can remove all the node_modules folders from the projects you don’t actively work on and free up a significant amount of disk space doing it.

Introduction

Whenever you work on a project that requires certain libraries or packages from npm (Node Package Manager), you might have noticed that it creates a “node_modules” on the root of your project. If this is new to you, node_modules stores copies of all the npm packages that you add to your project (using npm install, yarn add, etc.). Every single one of them is tied to a particular version, which is commonly stated in the “package.json” file which is created when a project is initialized using npm init.

But then all the packages that you use may also have their own dependencies that are stored in their node_modules folder all of which would be pinned to a certain version (again, defined in the package.json file of that dependency). As you might have realized, this is where the disc bloat happens. Each of these packages may have dozens of their own dependencies and so on, and it goes all the way down to the fundamental node libraries.

Image credits @MaciejRayMarcin

Of course, I’m simplifying the process, there is definitely some optimization done here. But I just tried to create an empty React project using create-react-app and the node_modules of this bare minimum React project alone was taking up 259 MB! I haven’t even added all the 500 packages I love to use yet!

Removing node_modules

Now that we understand how all the node_modules folders of your projects can take up a huge portion of your disc space, let us see how we can remove them.

Depending on what you want to do, you might want to remove all the node_modules in your disc or you might want to remove the ones that you no longer actively work on. Now you can purely use the command line to remove all the folders with the name “node_modules” but there’s a much better and safer way of doing this.

Using npkill

npkill is yet another npm package (oh the irony!) that lets you easily find and pick which node_modules you want to remove. You can install npkill globally in your system using the following command,

npm i -g npkill
Code language: Bash (bash)

Once installed, you can use the npkill CLI using the following command,

npkill
Code language: Bash (bash)

Once you run it, the node_modules folders from the current directory and its subdirectories are listed by default. When the list appears, you can use the arrow keys to move through it and the spacebar to delete a particular node_modules directory. It should look something like this,

npkill CLI

Look at some of those sizes! You may think it will take a while to remove these node_modules folders but that is not true, npkill is actually very fast! Once you’re done removing the ones you want, you can press q or control + c on your keyboard to exist out of the interface.

Alternatively, if you do not want to install npkill locally on your system you can use the following command,

npx npkill
Code language: Bash (bash)

npx lets you use npm packages without installing them locally on the system, pretty neat!

Will it break my projects?

No, deleting the node_modules folder from your projects is absolutely harmless. If you’ve used Git before you might have noticed that we always exclude the node_modules folder in the .gitignore file. That is because the metadata about all the packages in your project is stored in the package.json file and you can get back your packages anytime by running the npm install command in your project folder.

Using pnpm

There is an alternative to npm called pnpm that claims to be faster and more disk efficient than npm. According to its website, the files inside node_modules are cloned or hard-linked from a single content-addressable storage. As I tend to use mostly the same packages for a lot of my projects, this seems like a really good option to me.

You can install pnpm globally on your system with this command,

npm install -g pnpm
Code language: Bash (bash)

After that, the usage is very similar to npm itself. You can check out their docs for more details.

I personally haven’t really used pnpm much since I discovered it while I was researching the topic of this article. So I cannot really provide a solid review on how it will hold up if you use it for everything. But I think it is a great execution of an excellent idea and suggest you try it if you do not want to keep on removing node_modules folders after you’re done with a project.

Conclusion

The disk bloat issue is very apparent in the node community and I think it’s safe to say that the people at npm are actively trying to solve this or at least optimize it further. In this article, we discussed why and how node packages take up so much space in your computer and how we can remove the node_modules folder from the projects we are not actively working on and save disk space. We also briefly looked at pnpm which is supposed to be a better alternative to npm in terms of speed and disk space management. Now it is your turn to use what you’ve learned from this and free up some of your precious disk space!

If you have any questions regarding this article or want to talk about anything technology, you can find me on Twitter. Thank you for reading!

Sharing is caring

Did you like what Supantha Paul wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: