Sub topic 16
When to move to a monorepo with packages
This chapter assumes you have a feature-based React app with public APIs per feature (an index.ts file) and lint rules that enforce them. It also assumes you know npm and a bundler such as Vite. Most teams do not need a monorepo. This chapter helps you decide when you do, and shows what the first step looks like.
What a monorepo with packages is
A monorepo is one Git repository that holds several projects. A workspace package is a folder with its own package.json that other projects in the repository can depend on by name. The package manager links these folders together, so no publishing to a registry is needed.
Packages are the same idea as feature modules, with a stronger boundary. Inside one app, a deep import like @/features/cart/hooks/useCart is stopped only by a lint rule. Across packages, the package's exports field decides what can be imported, and the tool refuses the rest.
Signs you need one
Move to packages when you see these problems, not before:
- A second app (admin site, mobile web, marketing site) needs the same components or data code as the first.
- The shared UI folder has become a design system with its own release rhythm and owners.
- Lint rules keep being bypassed, and you want a boundary that cannot be bypassed.
- Build or test times are high enough that you want to run only what a change affects.
- Different teams need separate ownership, with review rules per folder.
If you have one app and one team, stay with folders and ESLint. A monorepo adds tooling, version rules, and build configuration that you must maintain.
Dependencies point one way: apps depend on packages, and packages never depend on apps.
A first step: extract shared UI
Your src/shared/ui folder is the usual first candidate. A layout with npm workspaces looks like this:
repo/
package.json
apps/
web/
package.json
src/
packages/
ui/
package.json
src/
index.ts
Button.tsxThe root package.json declares the workspaces:
{
"private": true,
"workspaces": ["apps/*", "packages/*"]
}The ui package names itself and limits what it exposes:
{
"name": "@acme/ui",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"peerDependencies": {
"react": "^18.0.0"
}
}The app lists the package as a dependency, using the * version so that npm links the local folder:
{
"name": "@acme/web",
"private": true,
"dependencies": {
"@acme/ui": "*"
}
}After you run npm install at the root, the app imports it like any library:
import { Button } from "@acme/ui";
export function SaveBar() {
return <Button>Save</Button>;
}Any import such as @acme/ui/src/Button fails, because exports lists only the root entry.
Common errors
- Peer dependency duplication. If
uibundles its own copy of React, hooks break with an "Invalid hook call" error. Declare React as a peer dependency, as shown above. - Source not compiled. Pointing
exportsat.tsfiles works only if the app's bundler compiles them. Vite does. Otherwise, build the package first. - Circular dependencies. A package that imports from an app, or two packages that import each other, cannot be built in order. Keep the graph one-way.
- Splitting too early. Packages with one consumer add cost and no benefit.
Exercise
Your team has one React app and one team of six developers. Developers sometimes import internal files from other features. Should you move to a monorepo? What do you do instead?
Show answer
No. The problem is a missing boundary, not a missing second consumer. Add the ESLint no-restricted-imports rule so imports must go through each feature's index file. Consider a monorepo when a second app needs shared code or when the shared UI needs its own ownership and release process.
Exercise
Why does @acme/ui declare React under peerDependencies instead of dependencies?
Show answer
With dependencies, the package manager may install a second copy of React for ui. Two copies of React cause "Invalid hook call" errors. A peer dependency makes ui use the React copy the app provides.