Speeding up the npm install

node.js, npm

Solution

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)

Problem

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.

Original source