Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache npm install Task in VSTS

I have configured a private agent in VSTS and have installed NPM there globally. When I'm trying to install NPM through my build task, it is still installing NPM packages for every build which is taking an aweful lot of time- approximately 12 minutes.

How can I cache the NPM installations so that the build time is reduced?

like image 592
Swayam Ghosh Avatar asked Oct 01 '18 09:10

Swayam Ghosh


1 Answers

We use npm-cache, npm-cache is a node module that will calculate a hash of your package.json file for every hash it will create zip folder on your build server with the content of node_modules, now npm install is reduced to extracting a zip on every build (of course only in case you didn’t actually change package.json).

The idea is: in the first time the tool download the npm packages and save them locally, in the second time if the package.json not changed he takes the packages from the local disk and copy them to the build agent folder, only if the package.json changed he downloads the packages from the internet.

  1. Install the npm-cache on the build machine:

    npm install npm-cache -g

  2. In the build definition add Command Line task (Tool: C:\Windows\User\AppData\Roaming\npm\npm-cache (or just npm-cache if you add the tool to environment path variables); Arguments:install npm; Working folder: $(Build.SourcesDirectory) (or where package.json located).

like image 107
Shayki Abramczyk Avatar answered Nov 10 '22 21:11

Shayki Abramczyk