Blog

Caching your Node modules in Azure DevOps

23 Aug, 2019

How can you make your builds complete faster so that you can build more often and have earlier feedback?
You could do this by caching your node modules in Azure DevOps.

NOTE: This article has been updated and moved to qxperts.io.

guest
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Michal
Michal
2 years ago

Gists are no longer working, could you update them?

Riccardo Corradin
Riccardo Corradin
2 years ago
Reply to  Michal

Hi Michal, sorry for the late reply. There was an issue with the blog platform in which I could not reply to comments. The Gists have been updated.

Markus Faßbender
Markus Faßbender
2 years ago

The biggest question: how to and when to invalidate the cache?

Cristiana Bardeanu
Admin
2 years ago

Hi Markus,
Yes, the behavior in this blog post is when the package lock file changes. This happens when you update, remove or add a package. You can toggle this with the “Key file” field.

Jader Oliveira
2 years ago

Hi,
Sorry, but the steps mentioned aren’t working. Is it possible to validate it?
Thank you for your time.

Cristiana Bardeanu
Admin
2 years ago
Reply to  Jader Oliveira

Hi Jader,
I tried to reach out to you some time ago on Github. Let me know if you still have problems. About three weeks ago I verified my solution and everything was working as it should.

Jader Oliveira
2 years ago

Extensions also aren’t working.
Also, I tried to use the official [caching approach](https://github.com/microsoft/azure-pipelines-yaml/blob/master/design/pipeline-caching.md), but didn’t work as well.
Any luck, let me know.

Asorak
Asorak
2 years ago

Node_modules can be OS-specific, if I remember correctly e.g node-gyp compiles during yarn install

Cristiana Bardeanu
Admin
2 years ago
Reply to  Asorak

Hi Asorak,
thanks for the addition. I checked this and found out that the node_modules folder is both the same on UNIX and Windows based machines. Maybe you could elaborate on a specific scenario? Thank you.

Amit
Amit
1 year ago

Hi Team,
While using above I am getting Warning “”The feed with ID ‘c3897c81-6c36-456c-94a3-0733a6298cdd’ doesn’t exist.”. I have tried using both the feed name and ID but still getting same error. The feed exists 🙂
Below is the task:
– task: RestoreAndSaveCache@1
inputs:
keyfile: ‘**/package-lock.json, !**/node_modules/**/package-lock.json, !**/.*/**/package-lock.json’
targetfolder: ‘**/node_modules, !**/node_modules/**/node_modules’
vstsFeed: ’95d1f24e-3554-47fd-a4d2-dd57a6537aee’
platformIndependent: true
Any help would be appreciated!
Thanks,
Amit

farid
farid
1 year ago

@all if you have problem
you must set artifact to organization not in project
https://github.com/microsoft/azure-pipelines-artifact-caching-tasks/issues/40#issuecomment-619001316

Srinivas
Srinivas
1 year ago

Hi I am trying to cache the modules but it always post-job and its not updated feed.
Skipped Post-job: RestoreAndSaveCache
Find below for my Yaml script:
steps:
– task: NodeTool@0
inputs:
versionSpec: ’10.x’
displayName: ‘Install Node.js’
– task: 1ESLighthouseEng.PipelineArtifactCaching.RestoreAndSaveCacheV1.RestoreAndSaveCache@1
inputs:
keyfile: ‘**/package-lock.json, !**/node_modules/**/package-lock.json, $(Build.SourcesDirectory)/package-lock.json’
targetfolder: ‘**/node_modules, !**/node_modules/**/node_modules, $(Build.SourcesDirectory)/node_modules’
vstsFeed: ‘*********-********-***********’
platformIndependent: true
– script: |
npm ci
displayName: Install dependencies only if there is no cache available
condition: ne(variables[‘CacheRestored’], ‘true’)

Cristiana Bardeanu
Admin
1 year ago
Reply to  Srinivas

Hi Srinivas,
Thanks for your message and for sharing your YAML code. I tried to replicate the issue and the first time I get an error In the restore/save node modules step: ##[warning]Error: Failed request: (503).
It also skips the post job like you mentioned.
However, when I run it again, it succeeds and doesn’t skip the post job anymore.
Another cause could be a wrong feedId. When this happens it will always skip the post job. You can find the feedId by adding the “Restore and save cache” task through the Azure DevOps UI.
Hope this helps.

Suraj Pardeshi
Suraj Pardeshi
7 months ago

Hi Ric,
I am using the Restore and save Cache task before npm install task in my Classic UI editor build pipeline.
I have provided same key files and target folder like you but after the build is executed i see a warning in the logs of estore and save Cache task like below,
##[warning]no key files matching: e:\Cache\Agent01\9\s\**\package-lock.json,!**\node_modules\**\package-lock.json,!**\*\**\package-lock.json
And also there is no cache saved in my artifacts feed.
What am I missing?

Explore related posts