I am trying to speed up the npm install during the build process phase. My package.json has the list of packages pretty much with locked revisions in it. I've also configured the cache directory using the command
npm config set cache /var/tmp/npm-cache --global
However, on trying to install using npm install -g --cache
, I find that this step isn't reducing the time to install by just loading the packages from cache as I would expect. In fact, I doubt if it's even using the local cache to look up packages first.
If you find that npm is running slow and it isn't your computer or internet, it is most likely because of a severely outdated version.
It takes us something like 25 minutes to get a master build done. In that process, npm install takes 16 minutes alone. Why is it that bad? The root cause is that npm , node and javascript motto of single responsibility principle leads to dependency hell and spaghetti effect.
clean: Delete all data out of the cache folder. Note that this is typically unnecessary, as npm's cache is self-healing and resistant to data corruption issues. verify: Verify the contents of the cache folder, garbage collecting any unneeded data, and verifying the integrity of the cache index and all cached data.
Proposing two more modern approches:
1) npm ci
Use npm ci
, which is available from npm version 5.7.0
(although I recommend 5.7.1
and upwards because of the broken release) - this requires package-lock.json
to be present and it skips building your dependency tree off of your package.json
file, respecting the already resolved dependency URLs in your lock file.
A very quick boost for your CI/CD envs (our build time was cut down to a quarter of the original!) and/or to make sure all your developers sit on the same versions of dependencies during development (without having to hard-code strict versions in your package.json
file).
Note however that npm ci
removes the node_modules/
directory before installing, so it won't benefit from any caching strategies.
2) npm i --prefer-offline
Use the --prefer-offline
flag with your regular npm install
/ npm i
. With this approach, you need to make sure you've cached your node_modules/
directory between builds (in a CI/CD environment). If it fails to find packages locally with the specific version, it falls back to the network safely.
You can also add --no-audit --progress=false
to reduce pre-install checks and remove the progress bar (latter is only a very slight improvement)
For pure npm solution, you may try
npm install --prefer-offline --no-audit --progress=false
Prefer offline may not be useful for the first run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With