Field Notes: Dependencies & Lockfiles
Practical lessons from teams that run JavaScript projects for years. Most "nothing changed but the build broke" incidents trace back to an unpinned input, an unreviewed lockfile change, or a dependency nobody remembers adding.
Pin the whole toolchain, not just packages
| Input | Pin it with |
|---|---|
| dependency versions | the lockfile, committed; npm ci / --frozen-lockfile / --immutable in CI |
| the package manager | "packageManager": "pnpm@10.34.7" (Corepack) |
| Node | .nvmrc / .node-version with an exact version, engines.node as a range, the CI image by digest |
| registry | a project .npmrc with registry= and scope mappings |
| install scripts | allowScripts / onlyBuiltDependencies, reviewed like code |
| global tools | none: every tool is a devDependency, run through scripts or npx |
A new developer should be able to run corepack enable && pnpm install && pnpm test and get the same result as CI.
Review lockfile diffs, don't skip them
GitHub collapses lockfile diffs by default, and that's where surprises hide: a "small" upgrade that pulled in 40 new transitive packages, a version that moved backwards, a package now resolved from a different registry. Turn the diff into a summary instead of reading thousands of lines:
mkdir -p svc && cd svc
npm init -y > /dev/null
npm install lodash@4.17.21 chalk@4.1.2 semver@6.3.1 > /dev/null
cp package-lock.json /tmp/lock.before.json
npm install semver@7 lodash@latest is-odd@3.0.1 > /dev/null
cat > lockdiff.mjs <<'JS'
import { readFileSync } from 'node:fs';
const load = (f) => Object.fromEntries(Object.entries(JSON.parse(readFileSync(f, 'utf8')).packages)
.filter(([k]) => k).map(([k, v]) => [k.replace(/^.*node_modules\//, '') + (k.split('node_modules/').length > 2 ? ' (nested)' : ''), v]));
const [a, b] = [load(process.argv[2]), load(process.argv[3])];
for (const name of [...new Set([...Object.keys(a), ...Object.keys(b)])].sort()) {
const [x, y] = [a[name], b[name]];
if (!x) console.log(`+ ${name} ${y.version}`);
else if (!y) console.log(`- ${name} ${x.version}`);
else if (x.version !== y.version) console.log(`~ ${name} ${x.version} -> ${y.version}${y.resolved?.startsWith('http://localhost:4873/') ? '' : ' (different registry!)'}`);
else if (x.resolved !== y.resolved) console.log(`! ${name} ${x.version} resolved from a new URL`);
}
JS
node lockdiff.mjs /tmp/lock.before.json package-lock.json+ is-number 6.0.0
+ is-odd 3.0.1
~ lodash 4.17.21 -> 4.18.1
~ semver 6.3.1 -> 7.8.5
Ten lines of script turn a lockfile diff into something a reviewer can judge in seconds: one major upgrade (semver 6 → 7, read its changelog), one minor, and a new direct dependency (is-odd) that brought a transitive one (is-number) with it. Tools like Renovate put the same summary into their pull requests.
Update deliberately, in small steps
- Automate the proposals, not the merges. Renovate or Dependabot opens pull requests; CI runs the full test suite; humans merge. Group low-risk updates (all
@types/*, all patch versions of dev tools) to reduce noise. - One major upgrade per pull request. When React 19 breaks something, you want to know it was React 19, not the other 30 packages in the same PR.
- Wait a few days before adopting brand-new releases. Most malicious or broken releases are found and pulled within days. npm's
min-release-age, Yarn'snpmMinimalAgeGateand Renovate'sminimumReleaseAgeall implement this. - Keep a cadence. A monthly "dependency day" keeps upgrades small; skipping a year turns them into a project.
Keep the tree small
Every dependency is code you ship, audit and update. Before adding one, ask:
| Question | Why |
|---|---|
| Is it a few lines I could write? | left-pad and is-odd are famous for a reason: a 3-line helper isn't worth a supply-chain link |
| How many transitive dependencies does it bring? | npm install x --dry-run or npm view x dependencies before installing |
| Is it maintained? | last publish date, open issues, bus factor |
| Does it run install scripts? | each one is code that runs on every developer machine and CI agent |
| Is it ESM, typed, tree-shakeable? | it affects bundle size and DX for years |
Periodically remove what you no longer use (knip, depcheck), and check for duplicates that bloat bundles (npm query '*' | jq …, pnpm dedupe, yarn dedupe).
Monorepo specifics
- One version per dependency across the repo: pnpm catalogs, Yarn constraints, or a lint script. Two Reacts in one bundle is a bug, two lodashes is waste.
workspace:for internal packages, never a registry version, so a missing local package fails loudly.- Hoisting is not a feature. If a package works only because something else hoisted its dependency, declare it; pnpm finds these for you.
- Run tasks from the root with filters (
pnpm --filter ...[origin/main],turbo --affected) so CI time scales with the change, not with the repo.
When the registry is down (or you're offline)
Your build depends on a registry being reachable unless you plan otherwise:
- A caching proxy (Verdaccio, Artifactory, Nexus) in front of npmjs means already-fetched versions keep working during an outage.
- CI caches (
~/.npm, the pnpm store, Yarn's cache) help, but are best-effort and evicted. - For air-gapped builds, Yarn's zero-installs or a vendored offline mirror (
yarn-offline-mirrorin classic,pnpm fetch+--offline) make installs registry-free.
Test yourself
Why summarise a lockfile diff instead of trusting the package.json diff?
- Lockfiles are smaller
- The lockfile shows every transitive change: new packages, major bumps, changed registries
- package.json diffs are hidden by GitHub
- It speeds up npm ci
What does waiting a few days before adopting a new release (min-release-age, npmMinimalAgeGate) protect against?
- Slow downloads
- Freshly published malicious or broken releases that are usually pulled within days
- License changes
- Peer conflicts
What is the best way to make a project's Node and package-manager versions reproducible?
- Write them in the README
- Pin them in files the tools read: .nvmrc/.node-version, packageManager, and a CI image by digest
- Install them globally on CI
- Use the latest versions always