Overview
Unlike traditional dependency management where each workspace package needs to independently specify versions, catalogs let you:- Define version catalogs in the root package.json
- Reference these versions with a simple
catalog:
protocol - Update all packages simultaneously by changing the version in just one place
How to Use Catalogs
Directory Structure Example
Consider a monorepo with the following structure:1. Define Catalogs in Root package.json
In your root-levelpackage.json
, add a catalog
or catalogs
field within the workspaces
object:
package.json
catalog
or catalogs
at the top level of the package.json
file, that will work too.
2. Reference Catalog Versions in Workspace Packages
In your workspace packages, use thecatalog:
protocol to reference versions:
packages/app/package.json
packages/ui/package.json
3. Run Bun Install
Runbun install
to install all dependencies according to the catalog versions.
Catalog vs Catalogs
Bun supports two ways to define catalogs:-
catalog
(singular): A single default catalog for commonly used dependenciesReference with simplypackage.jsoncatalog:
:packages/app/package.json -
catalogs
(plural): Multiple named catalogs for grouping dependenciesReference withpackage.jsoncatalog:<name>
:packages/app/package.json
Benefits of Using Catalogs
- Consistency: Ensures all packages use the same version of critical dependencies
- Maintenance: Update a dependency version in one place instead of across multiple package.json files
- Clarity: Makes it obvious which dependencies are standardized across your monorepo
- Simplicity: No need for complex version resolution strategies or external tools
Real-World Example
Here’s a more comprehensive example for a React application: Root package.jsonpackage.json
packages/app/package.json
packages/ui/package.json
packages/utils/package.json
Updating Versions
To update versions across all packages, simply change the version in the root package.json:package.json
bun install
to update all packages.
Lockfile Integration
Bun’s lockfile tracks catalog versions, making it easy to ensure consistent installations across different environments. The lockfile includes:- The catalog definitions from your package.json
- The resolution of each cataloged dependency
bun.lock(excerpt)
Limitations and Edge Cases
- Catalog references must match a dependency defined in either
catalog
or one of the namedcatalogs
- Empty strings and whitespace in catalog names are ignored (treated as default catalog)
- Invalid dependency versions in catalogs will fail to resolve during
bun install
- Catalogs are only available within workspaces; they cannot be used outside the monorepo
Publishing
When you runbun publish
or bun pm pack
, Bun automatically replaces
catalog:
references in your package.json
with the resolved version numbers.
The published package includes regular semver strings and no longer depends on
your catalog definitions.