Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean node_modules folder when prepping for deployment

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.

like image 941
Hayko Koryun Avatar asked Dec 21 '13 21:12

Hayko Koryun


2 Answers

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.

like image 59
Brett Postin Avatar answered Sep 20 '22 05:09

Brett Postin


Maybe you are interested in this little find command, which I've assembled over time. Please be aware, that this is not a "one-size-fits-all" solution! You need to check it very carefully against your requirements. It is intended for node.js environments and will definitely destroy browser environments. I run in a bash script as a postinstall script in npm.

DO NOT BLINDLY COPY 'N PASTE. You have been warned!

find node_modules \( \( -name "dist" -or -name "ts" -or -name "logos" -or -name "min" -or -name "test*" -or -name "doc*" -or -name "tst" -or -name "example*" -or -name "build" -or -name "man" -or -name "benchmark*" \) -and -type d \) -or \
   \( \( -iname "readme*" -or -iname "changelog*" -or -iname "notice*" -or -iname "test*.js" -or -iname "*.min.js" \) -and -type f \) -or \
   \( -path "*moment-timezone/data/unpacked*" -and -type d \)

To be sure, I've not added the final line | xargs rm -rf. You can safely execute the above command without deleting anything and later add the pipe to xargs with rm to make it really happen.

What does the find command do? I'll explain it pattern by pattern.

  1. \( \( -name "dist" -or -name "ts" -or -name "logos" -or -name "min" -or -name "test*" -or -name "doc*" -or -name "tst" -or -name "example*" -or -name "build" -or -name "man" -or -name "benchmark*" \) -and -type d \) => search for directories that match on the text in the quotes. The * is a wildcard.

  2. \( \( -iname "readme*" -or -iname "changelog*" -or -iname "notice*" -or -iname "test*.js" -or -iname "*.min.js" \) -and -type f \) => search for files in any folder where the text in the quotes matches regardless of the case. Especially the pattern "*.min.js" could be dangerous for some people.

  3. \( -path "*moment-timezone/data/unpacked*" -and -type d \) => remove the unpacked data from moment. This also saves plenty of space.

Feel free to improve this!

like image 34
Mario Mueller Avatar answered Sep 17 '22 05:09

Mario Mueller