Remove unnecessary files in node_modules

Josh Sherman
2 min read
Software Development Node.js

As the meme goes, the object with the largest mass in our universe is the node_modules directory.

Generally speaking, storage is cheap, and while an unkept set of dependencies can be quite messy (and often times a security hazard), the size of your node_modules directory tends to not be of major concern.

That said, if you’re working with a fixed-storage environment, like that of an AWS Lambda, the size of your dependencies can impact your ability to deploy new code.

On different occasions, I’ve attempted to throw some shell scripting at the problem, removing files that I know to be unnecessary for my project. Some of those file types and patterns include:

This list has served me well, but it’s limited to my own observations about which files are unnecessary for my projects. At the end of the day, the list is far from exhaustive, quite finite, and probably doesn’t change as often as it should.

Like most problems out there, fortunately somebody else has attempted to solve it, none other than TJ Holowaychuk.

The project I’m speaking of is node-prune.

Even though it’s a Node.js centric tool, it’s actually written in Golang and does require go to be installed to run.

To get things installed, simply run:

go install github.com/tj/node-prune@latest

And from your project’s directory (where you should have node_modules) you can run:

node-prune

Or tell node-prune where the node_modules you’d like to prune are:

node-prune ./path/to/node_modules

You will then be greeted with some friendly output letting you know how many files were analyzed, how many files were removed, how much space was recovered and how much time it took!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles