Speed Up NPM Build in Jenkins Speed Up NPM Build in Jenkins docker docker

Speed Up NPM Build in Jenkins


You should be using package caching but not caching node_modules directly. Instead you mount cache directories that your package installer uses, and your install will be blazing fast. Docker does make that possible by allowing you to mount directories in a container, that persist across builds.

For yarn mount ~/.cache or ~/.cache/yarn
For npm mount ~/.npm

docker run -it -v ~/.npm:/.npm ~/.cache:/.cache /my-app:/my-app testing-image:1.0.0 bash -c 'npm ci && npm test`

Note: I'm using npm ci here, which will always delete node_modules and reinstall using exact versions in the package-lock.json, so you get very consistent builds. (In yarn, this is yarn install --frozen-lockfile)


You could set up a Http proxy and cache all dependencies (*)(**).

Then use --build-arg to set HTTP_PROXY variable:

docker build --build-arg HTTP_PROXY=http://<cache ip>:3128 .

*: This will not work improve performance on dependencies that need to be compiled (ie: c/c++ bindings)

**: I use a Squid container to share cache configuration


In my case it was a bunch of corporate software installed in my computer apparently some anti virus analyzing all the node_modules files from the container when I mounted the project folder on the host machine, what I did was avoid mounting node_modules locally. Immediately sped up from 25 min to 5.