How to clean node_modules folder when prepping for deployment

Hayko Koryun picture Hayko Koryun · Dec 21, 2013 · Viewed 12.3k times · Source

How would I go about cleaning the node_modules folder when prepping my code for deployment.

I am making an app using node-webkit and would prefer to include the least amount of files possible when bundling the final version of the app as the unzip process takes some time.

I've looked at npm dedupe and use npm install --production to get rid of duplicates and fetch only production files, however I am still left with Readme files, benchmarks, tests and build files which I don't need.

What I would like to end up with for each module in the node_modules folder is a LICENSE file if it exists, the package.json and anything else that I need for the module to run, but nothing more.

The question: How to automatically clean a node_modules directory for a SCM commit was heading somewhat in the same direction, but it is talking about committing so not really what I am looking for.

The question: NPM clean modules again was somewhat along the same lines as mine, but not quite fully there.

This answer helps as it is more efficient version for dedupe for bundling the final app.

Update
I tried the custom module linked from here but it didn't seem to work correctly, even after some fiddling about.

With all that said, I haven't quite found the right answer yet.


Here's an example of what I am looking for.

In my project I currently have two dependencies: socket.io and socket.io-client.

Together they make up 15 MB with 550 files in 110 folders.

Manually cleaning readme, makefile, VC++ build files such as .pdb and .obj and other unnecessary files I was able to shrink it down to 2.74 MB with 265 files in 73 folders.
This is with just two modules.

I would like to find out if there is a way to do this automatically, preferably with npm.

Answer

Brett Postin picture Brett Postin · Dec 1, 2015

This module attempts to intelligently cleanup the node_modules folder:

modclean

Install:

npm install modclean -g

or

npm install modclean --save-dev

Usage:

modclean

It uses a set of default patterns to remove unnecessary bloat from modules throughout the dependency tree.