Monorepos promise cohesive versioning, atomic commits, and effortless code reuse across services. In practice, most growing engineering teams experience the exact opposite: circular dependency tangles, accidental tight coupling, and CI pipelines that rebuild the world whenever someone updates a documentation typo.
When a simple change in @acme/ui invalidates the cache for your Fastify backend, your dependency graph is broken. Let's look at how to structure an enterprise-grade full-stack TypeScript monorepo using pnpm and Turborepo to enforce strict boundaries and guarantee sub-minute CI runs.
1. Enforce Tiered Layer Isolation
The most common monorepo mistake is treating the packages/ directory as an unstructured dumping ground. Without clear architectural tiers, features start importing other features, leading to spaghetti graphs and cache invalidation cascades.
A resilient topology separates concerns into four explicit layers:
acme-monorepo/
├── apps/
│ ├── api/ # Fastify / Node.js runtime
│ └── web/ # Next.js customer application
├── packages/
│ ├── features/ # High-level domain logic
│ │ ├── auth/ # RBAC, session checks
│ │ └── billing/ # Stripe integration
│ └── platform/ # Low-level primitives (agnostic)
│ ├── database/ # Prisma/Drizzle schema & client
│ └── ui/ # Shared Radix + Tailwind components
└── tooling/
├── eslint-config/ # Shared lint configurations
└── typescript-config/ # Shared tsconfig bases
The Golden Boundary Rule: Higher layers may import from lower layers, but never the reverse.
-
apps/webcan import@acme/billingand@acme/ui. -
@acme/billing(feature) can import@acme/database(platform). -
@acme/databasemust never import fromfeaturesorapps.
You can enforce this with ESLint rules (like eslint-plugin-boundaries) to fail the build immediately if someone attempts a cross-domain illegal import.
2. Standardize Package Exports and TS References
To prevent slow internal bundling steps during local development, use modern conditional exports with TypeScript 5.x project references or bundler-mode transpilations.
Inside your shared package (packages/platform/ui/package.json):
{
"name": "@acme/ui",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./src/index.ts"
}
},
"scripts": {
"lint": "eslint .",
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
"react": "^18.0.0"
}
}
In apps/web/next.config.mjs, tell the host application to transpile internal packages directly:
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ["@acme/ui", "@acme/auth"],
};
export default nextConfig;
This removes the need for watch tasks, build artifacts (dist/), and intermediate .d.ts compilation during feature development.
3. Pipeline Fine-Tuning in turbo.json
Turborepo calculates cache hashes based on file contents, environment variables, and declared dependencies. If your task inputs are too broad, you destroy cache hit rates. If they are too narrow, Turborepo serves stale output.
Here is a production-hardened turbo.json configuration:
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [".env*"],
"tasks": {
"topo": {
"dependsOn": ["^topo"]
},
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**", "dist/**", "build/**"],
"inputs": ["src/**", "public/**", "package.json", "tsconfig.json"]
},
"test": {
"dependsOn": ["^build"],
"inputs": ["src/**", "test/**", "**/*.test.ts", "**/*.test.tsx"]
},
"lint": {
"inputs": ["src/**", ".eslintrc*", "eslint.config.*"]
},
"typecheck": {
"dependsOn": ["^topo"],
"inputs": ["src/**", "tsconfig.json"]
}
}
}
Notice that .next/cache/** is explicitly excluded from outputs; letting Next.js pack its own volatile cache into Turborepo's tarball balloons artifact sizes and slows remote downloads over CI runners.
Take It to Production
Architecting a high-speed repository doesn't stop at package folders. Real enterprise setups need optimized Docker multi-stage builds that prune monorepo workspaces, GitHub Actions workflows with remote cache authentication, and clean versioning pipelines via Changesets.
If you want complete, copy-paste implementations covering all of these edge cases, check out The Enterprise Monorepo Playbook: Turborepo, pnpm Workspaces, Shared Packages & CI Caching Architecture ($22).
The full guide includes production Dockerfiles for Node backends and Next.js, GitHub Actions CI templates with Turborepo remote caching, automated package release workflows, and real-world reference topologies for full-stack engineering teams.
Top comments (0)