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.081suser 0m18.062ssys 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 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
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
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.893suser 0m4.049ssys 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.