Lesson 3 of 9 · 25m
Internal Dependencies, Boundaries, and Versioning
Model internal packages as products with explicit APIs, dependency direction, compatibility expectations, and release semantics.
The quickest way to ruin a monorepo is to treat internal packages as convenient folders. They are still dependencies. They need contracts, owners, compatibility rules, and a reason to exist. The fact that you can import any file in the repository does not mean you should.
The dependency direction is architecture
Draw the direction before you write the import:
applications -> product libraries -> platform libraries -> primitivesThe arrow means "may depend on." A primitive logging package must not import application configuration. A UI package must not import a server-only database client. If the graph acquires a cycle, builds become order-sensitive, test selection becomes pessimistic, and ownership becomes muddy because either side can break the other.
Cycles are not merely ugly. In affected builds, a cycle makes changes appear relevant in both directions. In deployment, it becomes impossible to reason about which artifact is independent.
Public APIs should be narrow
In TypeScript packages, expose an intentional entrypoint:
{
"name": "@acme/auth",
"exports": {
".": {
"types": "./src/index.ts",
"import": "./src/index.ts"
}
}
}Then export only stable surface area from src/index.ts. Deep imports like @acme/auth/src/internal/token-cache bypass the package contract. They make refactors dangerous because every consumer becomes coupled to file layout rather than behavior.
If your tooling permits, block deep imports with lint rules or module-boundary rules. Convenience imports are how internal packages become impossible to change.
Versioning inside a monorepo is still real
Internal versions answer a different question than public versions. They communicate compatibility and release coordination, not marketing. A typical workspace uses fixed internal references during development:
{"dependencies": {"@acme/auth": "workspace:*"}}But publishing or packaging deployable artifacts requires a decision: pin exact versions, use workspace resolution, or publish immutable internal artifacts. Do not improvise per package. Mixed semantics make lockfiles lie by omission.
Breaking changes need migration mass
In a polyrepo world, a breaking library change can sit unadopted for months. In a monorepo, the temptation is to update every consumer atomically. That is often right, but only if the change is small enough to validate. For broad changes, use a compatibility bridge:
1. Add the new API.
2. Migrate consumers incrementally.
3. instrument or lint old usage.
4. Remove the old API after usage reaches zero.This is not bureaucratic caution. It is how you avoid thousand-file pull requests that nobody can review honestly.
Generated code is a dependency too
Generated OpenAPI clients, protobuf output, database types, and schema artifacts should have clear producers and consumers. Commit generated output only when it makes review and offline builds easier; otherwise generate it as a build step. Either way, model the generation dependency explicitly. Hand-editing generated files creates a divergence that will eventually erase trust in the graph.
Field notes
- Internal packages without owners become dumping grounds with export statements.
- A dependency cycle found by tooling is a design defect, even if the build currently passes.
- Deep imports feel faster for one pull request and slower for the next year.
- Atomic migration is excellent for small refactorings and dangerous for broad behavioral changes; choose the bridge deliberately.
Was this lesson helpful?