2

With GitLab I can cache node_modules/ and/or I can cache the argument to npm ci --cache <dir>

What's the difference between these two options?

  • Should I cache node_modules/ between jobs and stages in my pipeline?
  • Should I cache just the <dir> provided to npm ci --cache
  • Should I cache both?

The GitLab docs show:

npm ci --cache .npm --prefer-offline

But they don't explain why I would not want to cache node_modules/.

Size difference:

❯ du -hs .npm
136M    .npm
❯ du -hs node_modules
932M    node_modules
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

1 Answers1

1

The difference in my case was massive, by NOT caching node_modules/ I reduced my CI pipeline timing between 30-50%.

It's faster to have every stage include

npm ci --cache .npm --prefer-offline

Then it is to move around node_modules/ with a GitLab cache layer.

I suppose the data may work out differently if not using containerized runners.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • But even with a cache, `npm ci` is still checking all the dependencies, for me taking 50 seconds (down from 70 seconds without cache). The download-time seems to be less relevant than the version comparison. Are you using npm in a regular repo with one single node_modules or in a workspaces monorepo? Have you tried just caching node_modules WITHOUT running `npm ci`? multiple people have reported doing that makes their build unstable but I want to know details how so and if that can be prevented. – Phil May 26 '23 at 07:30