Home » Nodejs » Speeding up the npm install

Speeding up the npm install

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

As suggested by @Daniel Serodio

You could also include your node_modules folder inside your repository but you should probably zip it first than add to repo, and while installing you can unzip it and just

 npm rebuild

(which works cross platform) it is quite fast.

This would also give you the benefit of full control over all your dependencies.

Also you can set the process flag to false to increase your speed by 2x.

npm set progress=false

Read source for more info

Questions:
Answers:

As very modern solution you can start to use Docker.
Docker allows you virtualize and pre-define as image the current state of your code, including installed npm-modules and other goodies.

Once the docker image for your infrastructure/env is built locally, or retrieved from remote repository, it will be stored on the host machine, and you can spin server in seconds.
Another benefit of it is that you use same virtualized code infrastructure on any machine where you deploy your code.
Docker speeds up install/deployment processes and is widely used technology.

To start using docker is enough to (all the snippets are just mock/example for pre-setup and are not by any means most robust/elegant solution) :

  1. Install docker and docker-compose using manuals and get some basic understanding of it at https://www.docker.com/
  2. Write Dockerfile file in root of your application

    FROM node:6.9.5
    RUN mkdir /usr/local/app
    WORKDIR /usr/local/app
    COPY package.json package.json
    RUN npm install

  3. create docker-compose.yml in the root of your project with such content:

    version: "2"
    server:
    hostname: server
    container_name: server
    image: server
    build: .
    command: sh -c 'NODE_ENV=development PORT=8080 node app.js'
    ports:
    - "8080:8080"
    volumes: #list of folders and files to use
    - ${PWD}/server:/usr/local/server
    - ${PWD}/app.js:/usr/local/app.js

  4. To start server you will need to docker-compose up -d. To see the logs docker-compose logs -f server. If you will restart your server it will do it in seconds once it built the image already at once.
    Then it will cache build layers locally so next run will take only few seconds.

I know this might be bit of a robust solution, but I am sure it is have most potential/flexibility and is widely used in industry. And while it requires some learning for anyone who did not used Docker before, in my humble oppinion, it is the best one for your problem.

Questions:
Answers:

You could also include your node_modules folder inside your repository (you are probably using git), and just npm rebuild (which works cross platform) on build/deploy processes, and is pretty fast.

This would also give you the benefit of full control over all your dependencies (I know that’s what shrinkwrap usually should be used for)

Edit:

Also you can set the progress flag to false to increase your speed by at least 20%. This works only with [email protected], and there will be hopefully fixes for that soon (see second link)

npm set progress=false
Questions:
Answers:

We have been trying to solve this problem to speed up our deployments.

We have settled on using pac, which follows the principles in the other answers. It zips the npm modules and includues them in your repo so you don’t have a million files in your commits and code reviews and you can just unzip/rebuild for the target machine.

https://www.npmjs.com/package/pac

Questions:
Answers:

Nothing helped me more than disabling antivirus (Windows Defender in my case) I got from 2:30 to 1 minute.
With npm-cache package I got to ~30 secs.
I tried to use yarn, which is very fast, but was randomly failing in my case.