Azure DevOps: Caching Node Modules

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

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

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

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!

Freelance Software Engineer