Lesson 5 of 9 · 21m
Bun Install, Lockfiles, and Workspace Discipline
Use Bun's package manager without losing reproducibility: lockfile behavior, script execution, workspace layout, and CI pinning.
Package managers are supply-chain and reproducibility tools before they are speed tools. Bun's installer is fast enough that teams sometimes stop thinking about it. That is backwards. The faster dependency installation becomes, the easier it is to mutate environments carelessly. Your job is to keep resolution deterministic while enjoying the speed.
Treat bun.lock as a release artifact
Bun uses its own lockfile format. Commit it, review it, and use frozen installation in automation:
bun install
git add bun.lock package.json
bun install --frozen-lockfileThe frozen mode matters. CI should fail when package.json and the lockfile disagree instead of quietly resolving a new dependency graph. Production images should do the same. Determinism is what lets you compare two deployments honestly.
Understand script side effects
Lifecycle scripts are a security boundary:
{
"scripts": {
"postinstall": "node scripts/download-model.js"
}
}A dependency install can execute code with the permissions of the installing user. Bun supports controlling lifecycle script execution, and organizations should decide that policy explicitly rather than leaving it as folklore. On a hardened CI runner, review unexpected install scripts before enabling them.
Workspaces need ownership rules
Bun workspaces are useful for small monorepos:
{
"workspaces": [
"apps/*",
"packages/*"
]
}The practical problems appear in deployment boundaries. If apps/api depends on packages/shared, your production artifact must either bundle the workspace dependency or copy enough of the workspace for resolution to work. A container build that only copies apps/api often breaks because the symlinked package points outside the copied tree.
FROM oven/bun:1 AS deps
WORKDIR /app
COPY package.json bun.lock ./
COPY apps/api/package.json apps/api/package.json
COPY packages/shared/package.json packages/shared/package.json
RUN bun install --frozen-lockfileNotice the shape: dependency metadata is copied before source so Docker layer caching works, and every workspace package needed for resolution is present.
Version pinning is operational hygiene
Pin Bun itself in CI and production:
container:
image: oven/bun:1.2.21Do not let local development float on a manually installed binary while automation uses another version. Runtime, bundler, installer, and test behavior all live in that one version.
Field notes
- Migrating from
package-lock.jsonorpnpm-lock.yamlis not just deleting files; compare resolved dependency versions before trusting the new graph. --frozen-lockfilebelongs in CI and production builds, not only in documentation.- Postinstall scripts are executable supply-chain surface. Inventory them, especially after large dependency updates.
- Workspace deployment failures are usually packaging failures, not TypeScript failures. Build the exact production layout locally.
Was this lesson helpful?