I would like to cache npm dependencies in a file in my Continuous Integration so that subsequent npm ci
invocations are fast. Also, when dependencies slightly change, I would like npm ci
to be pretty fast by reusing the cached dependencies from the previous build, and then store the cache with only the most recently used dependencies (without old unused dependencies). Is there a way to prune the npm cache to only the files that were used in the most recent npm ci
?
I see that GitHub’s actions/setup-node stores the ~/.npm
HTTP cache in GitHub’s cache, but since the primaryKey
of the cache.restoreCache
call is based on the hash of package-lock.json
, the entirety of ~/.npm
is regenerated if package-lock.json
is even slightly changed (cache-restore.ts). In contrast, I would like to use the previous ~/.npm
as the starting point even if it has slightly different dependencies.
I also saw a blog post, “Super fast npm install on Github Actions” by Selwyn which uses a liberal restore-keys
parameter to actions/cache
to reuse the previous ~/.npm
directory even when package-lock.json
changes. However, it appears that this solution fetches the old ~/.npm
cache but does not prune the ~/.npm
cache to only the recently used entries, so it will accumulate new versions of a dependency and not delete old versions. I would like a solution that prunes the cache to the entries that are needed by the current build.
Feel free to give an answer for pnpm
or yarn
too if there is an answer.