July 29, 2016

NPM Install Speed Up

I have been working on improving the speed in my deployment scripts, usually Front-End builds on large application can become incredible slow, aditionally install node modules npm install on every deploy is a total pain.

A quick solution to avoid installing and compiling node modules would be to package the node_modules folder save it to a artifactory service or a shared dev server so we can get back the package a rebuild these modules.

The Problem: npm install

For a basic angular project using webpack npm install in my computer takes about 1 minute 11 seconds:

time (npm install > /dev/null)
 
real 1m11.081s
user 0m18.062s
sys 0m5.616s
Solution: Package modules

Before compressing the modules we should make sure we clean up all we are not using in order to reduce the size of the package, to do so we can use npm prune

npm prune
tar -zcvf cache.tar.gz  node_modules
Solution: Use Packaged modules:

On our deployment server we can just get the cache file cache.tar.gz some dependencies like node-sass requires to get recompiled when some of its dependencies changes. so we need to rebuild the sources to match with the current node version.

tar -zxvf cache.tar.gz
npm rebuild

Also a good factor to reduce the time while waiting on install or rebuild is disable the progress bar.

npm set progress=false
The Result

After caching the modules, extract and rebuild the time was reduced to 6 seconds:

time (tar -zxvf cache.tar.gz > /dev/null && npm set progress=false && npm rebuild > /dev/null)
 
real 0m5.893s
user 0m4.049s
sys 0m2.107s

Hope this can help you to increase the speed of your deployments script and keep on the  continuous delivery (CD) approach. Thanks for reading.

Williams Medina
About Williams Medina
I am a Senior Front End Software Engineer, I've spent about 7 years working from different companies and gathering many experiences about software programming...