We all know that downloading dependencies with npm can be very time consuming, specially when we are limited to old npm versions.
For me, as a developer, this wasn't such a big deal because I had to do this very few times on my local development machine and everything worked with the node_modules cache in my project's folder. But now I want to take this the applications to a CI environment, with Jenkins.
I realized a huge ammount of time was spent on downloading dependencies with npm. This is a problem because:
-
npm downloads the dependencies in the project's folder, not a global folder such as Maven's /home/user/.m2
-
I have to clean up the Jenkins workspace folder in every run to avoid issues with the git checkout.
I want a very elegant solution for caching the npm dependencies on my Jenkins slaves, but so far I can only think of:
-
Removing everything but the node_modules folders from the Jenkins workspace. I don't like this because I could consume lots of HDD if I keep creating branches for my project. Each branch creates a workspace.
-
doing something like
cp ./node_modules /home/npm_cache
after every npm install and thencp /home/npm_cache ./node_modules
after the code checkout.
I feel these solutions are terrible. There must be a better way to do this.
Best Answer
What I have done in my Jenkins pipeline for 3 different projects is using
tar
instead ofcp
and thennpm install
instead ofnpm ci
, for each:cd
to your projectnpm i
tar cvfz ${HOME}/your_project_node_modules.tar.gz node_modules
Then in the pipeline:
Of course it has the disadvantage that with time dependencies change and the install will take longer, but I've managed to reduce my disk space usage in the image by about 0.5GB and
tar
is much faster thencp
(cp
~30 sec,tar
~5 sec)Total install time went in my case from about 3 minutes to a matter of seconds.