Monorepos and shared packages
What you need to know
A monorepo keeps multiple packages in one version-control repository. Use it when shared contracts or design components need coordinated change; a workspace is not an excuse to share every implementation detail.
By the end, you can: choose package boundaries; avoid cyclic dependency ownership.
Declare a local package for framework compilation
import type {NextConfig} from 'next';const config: NextConfig = {transpilePackages: ['@river/ui']};export default config;Expected behavior: A reviewed workspace package named @river/ui can be processed by the Next.js build. This config alone cannot resolve a missing dependency.
How it works
A package should expose a small stable surface through its package exports. Keep pure types and validators independent of React, Node and database drivers when possible. A design-system package owns presentation; an application owns routing, data access, configuration and authorization. A server package must not leak its internals through a barrel file imported by Client Components. Ensure one compatible React installation per renderer context and align peer dependencies with the actual application version. Workspace dependency declarations and lockfiles govern resolution; Next.js transpilePackages tells the bundler to process selected dependencies, including local source packages. It does not install dependencies, configure TypeScript aliases or repair conflicting peer ranges. Test the production artifact as well as local workspace development: build tracing may need explicit roots for files outside the app directory. Prefer a single app with feature folders until separate package ownership or reuse is real. Detect dependency cycles using declared imports and package exports, not folder names alone.
A one-direction package inventory
{ "apps/web": [ "packages/ui", "packages/contracts", "packages/data-server" ], "packages/ui": [ "packages/contracts" ], "packages/data-server": [ "packages/contracts" ], "packages/contracts": []}Expected behavior: No package depends back on apps/web. The client entry point must not import data-server. The graph is a planning artifact.
Common mistake
Do not export server credentials, database adapters and UI primitives from one unrestricted shared index. Transpilation does not create a trust boundary.
Practice and verification
Add a proposed dependency from contracts back to web. Explain the cycle, then move the shared type down into contracts instead. Run the package graph exercise with a disconnected cycle and a missing package.
Scope: Proposed Next.js 16.x / React 19; App Router, Cache Components disabled unless noted. Framework and browser examples are staged, not executed. No exact patched dependency release or live deployment verified.