Azure DevOps: Caching Node Modules

Sami C.
2 min readMay 7, 2020

--

Pipeline caching can help reduce build time by allowing the outputs or downloaded dependencies from one run to be reused in later runs, thereby reducing or avoiding the cost to recreate or redownload the same files again. Caching is especially useful in scenarios where the same dependencies are downloaded over and over at the start of each run. This is often a time consuming process involving hundreds or thousands of network calls.

Our goal

We’ll simply cache our node_modules so we don’t have to run ‘npm install’ every time we make a build on azure DevOps.

Let’s get into it!

Step 1: Add the caching task

Search for ‘cache’ and select the first one
You should get this screen

Step 2: Filling in the fields

Key

npm | “$(Agent.OS)” | $(Build.SourcesDirectory)/YOURAPP/FRONTENDFOLDER/package-lock.json

Be sure to set the right directory referencing your package-lock.json. This ensures the key will be different if we installed new packages!

Path

$(Build.SourcesDirectory)/YOURAPP/FRONTENDFOLDER/node_modules

Cache hit variable

CacheRestored

That’s all for the cache task! Let’s move on to our ‘npm install’ task

Step 3: Configuring npm install

In my example I’m using bash for my npm install… Add a custom condition

Add custom condition:

Set the custom condition to: ne(variables[‘CacheRestored’], ‘true’)

That’s all! You should now see the first build execute a npm install:

The second time it should skip the npm install:

That’s about one minute saved, awesome!

Any questions? feel free to post them down below.

More information can be found on:

Happy programming!

--

--

Sami C.
Sami C.

Written by Sami C.

Freelance Software Engineer

Responses (2)