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:
- Markdown files:
*.md
*.mkd
*.markdown
- READMEs
*readme*
- LICENSEs:
*license
license-*
- Documentation and examples:
doc
docs
example
examples
- Tests and test coverage:
test
tests
coverage
*-spec.*
*.spec.*
*-test.*
*.test.*
- Archives:
*.zip
*.tgz
*.tar.gz
- Hidden files and directories:
.*
- Build files and configurations:
makefile
,gulpfile.js
gruntfile.js
karma.conf.js
tslint.json
*eslintrc.*
- Source files:
*.c
*.cpp
*.h
- TypeScript files:
*.ts
tsconfig.json
- Pre-build distributions:
aws-sdk/dist*
react-native
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/[email protected]
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!