Skip to content
Latchkey

How to Publish Monorepo Packages in Topological Order

Publishing dependencies before dependents means a consumer installing a dependent always finds the dependency version it declares.

If @acme/app requires @acme/core@^1.3.0, publishing app first would briefly point at a nonexistent core version. Tools sort the workspace graph topologically so leaves publish first.

How each tool orders

ToolOrdering behavior
changeset publishPublishes in dependency order automatically
pnpm publish -rRespects workspace topology
yarn foreach --topologicalExplicit topological flag
nx release publishUses the Nx project graph

Yarn example

Terminal
# Publish leaves first, dependents last
yarn workspaces foreach -A --no-private --topological npm publish --tolerate-republish

Gotchas

  • A cyclic dependency between packages breaks topological ordering; keep the graph acyclic.
  • Even with correct ordering, a mid-run failure can leave a partial release, so pair ordering with retry-safe (idempotent) publishing.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →