Caching node_modules in CI
What you'll learn: How to dramatically speed up your CI pipeline by caching dependencies between runs instead of reinstalling them every time.
The Problem: Repeated Installations Waste Time
Every time your CI pipeline runs type-checking with tsc --noEmit, it first needs to install all your dependencies. Without caching, this means downloading and installing potentially hundreds of packages from npm every single run—even when your package.json hasn't changed at all.
Analogy: It's like repurchasing all your groceries every time you cook, even though most items in your pantry haven't expired. You're wasting time and money on something you already have.
How Dependency Caching Works
CI systems can save your node_modules folder (and related package manager files) after the first successful installation. On subsequent runs, if your package.json and lock file haven't changed, the CI system restores the cached dependencies instead of downloading everything again.
This turns a 2-3 minute installation step into a 10-20 second cache restoration.
Cache Keys: The Critical Piece
The cache is identified by a "key"—typically a hash of your lock file (package-lock.json, yarn.lock, or pnpm-lock.yaml). When the lock file changes (meaning dependencies changed), the key changes, and CI knows to install fresh dependencies and create a new cache.
Common Cache Targets
node_modules/— the installed packages~/.npmor~/.cache— package manager cache directories- Lock files determine when to invalidate the cache
Key Takeaway: Caching node_modules based on your lock file hash transforms slow CI type-checking jobs into fast ones by eliminating redundant package installations when dependencies haven't changed.