As the meme goes, the object with the largest mass in our universe is the
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:
- Markdown files:
- Documentation and examples:
- Tests and test coverage:
- Hidden files and directories:
- Build files and configurations:
- Source files:
- TypeScript files:
- Pre-build distributions:
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
Even though it’s a Node.js centric tool, it’s actually written in Golang and
go to be installed to run.
To get things installed, simply run:
go install github.com/tj/[email protected]
And from your project’s directory (where you should have
node_modules) you can
node-prune where the
node_modules you’d like to prune are:
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!