Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up the npm install

Tags:

node.js

npm

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.

like image 238
Prashanth Avatar asked Feb 27 '14 18:02

Prashanth


People also ask

Why my npm install is slow?

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.

How much time npm install takes?

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.

Is it safe to clean npm cache?

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.


2 Answers

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)

like image 93
kano Avatar answered Sep 22 '22 04:09

kano


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.

like image 33
Lee Chee Kiam Avatar answered Sep 19 '22 04:09

Lee Chee Kiam