Scalable frontend architecture: React, React Native, strict TypeScript and Turborepo
How to design a monorepo that shares real code between web and mobile, keeps TypeScript strict across all packages, and doesn't collapse under its own weight. Real challenges, real decisions, mistakes included.
There's a fantasy circulating in the JavaScript ecosystem: the monorepo as a universal solution. One repo, one pipeline, infinite shared code, coordinated builds, and everyone lived happily ever after.
The reality is more interesting. A monorepo solves real problems and creates others that nobody mentioned in the blog post that convinced you to do it. This article is about both sides.
The problem that leads you to a monorepo
The classic scenario: you have a React web app and a React Native mobile app. They share business domain, types, validation logic, constants, utilities. But they live in separate repos.
What happens over time:
- Types fall out of sync. The backend updates an endpoint, web adapts it, mobile finds out two sprints later.
- Validation logic exists twice, evolving divergently.
- Making a change that affects both platforms means two PRs, two pipelines, two manually coordinated deploys.
- Knowledge of what each part does exactly lives in specific people's heads, not in the code.
Fragmentation has a cost paid in hard-to-trace bugs and delivery speed that decreases over time.
Why Turborepo and not the alternatives
Nx is powerful but opinionated to the point of feeling like adopting a framework, not a tool. If your team has no prior experience with it, the learning overhead is real.
Lerna had its moment. Today it's mainly in maintenance mode.
pnpm workspaces alone works well for simple repos, but without intelligent caching, build time grows linearly with the size of the monorepo.
Turborepo does one thing well: caches task outputs and parallelises builds respecting the dependency graph. It's framework-agnostic, fast to set up, and has very little opinion on how you structure your packages.
// turbo.json
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "dist/**"]
},
"typecheck": {
"dependsOn": ["^typecheck"]
},
"lint": {}
}
}
The key is "dependsOn": ["^build"]: when you build the web app, Turborepo first builds all workspace packages it depends on, in the correct order, with cache.
The structure that scales
After several attempts, the structure that scales best is:
apps/
web/ # Next.js
mobile/ # Expo / React Native
packages/
ui/ # shareable components (only the ones that truly are)
domain/ # types, validations, pure business logic
config/ # base tsconfig, base eslint
utils/ # utilities with no platform dependencies
What can be shared (and what can't)
This is the most important lesson: not everything should be shared.
Share without issue:
- TypeScript types (
User,Product,ApiResponse<T>) - Pure validation logic (
validateEmail,parseDate) - Domain constants (
ROLES,STATUS_CODES) - Side-effect-free utilities (
formatCurrency,debounce) - Pure business logic hooks (no
react-nativeimports, no browser APIs)
Don't share (even if it hurts):
- Styled components: Tailwind doesn't exist in React Native, RN's StyleSheet doesn't exist on web
- Hooks touching platform APIs (
AsyncStoragevslocalStorage) - Navigation: React Navigation ≠ Next.js Router
- Animations: Reanimated ≠ Framer Motion
The most common mistake is trying to share UI components and ending up with an abstraction layer that serves neither platform well.
Strict TypeScript as a contract between packages
In a monorepo, TypeScript stops being an individual quality tool and becomes the contract between packages. This has important implications.
One base tsconfig to rule them all
// packages/config/tsconfig/base.json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitReturns": true,
"exactOptionalPropertyTypes": true,
"verbatimModuleSyntax": true,
"moduleResolution": "bundler",
"target": "ES2022",
"lib": ["ES2022"]
}
}
Each package extends this base and adds what's specific to its context:
// apps/web/tsconfig.json
{
"extends": "@repo/config/tsconfig/nextjs.json",
"include": ["src", "next.config.ts"]
}
The noUncheckedIndexedAccess problem
This option, which I recommend enabling, makes array[0] typed as T | undefined instead of T. Correct from a types standpoint, but it breaks code that assumed the first element always exists.
In a monorepo, enabling this in the base breaks all packages at once. The strategy: enable it package by package, starting with those that have the smallest code surface.
Shared types vs API contracts
There's a temptation to put API response types directly in packages/domain. The problem: when the backend changes, you break the frontend build before the change reaches production.
What works better: domain types in packages/domain, API response types generated automatically from an OpenAPI/GraphQL schema. That way the contract is the API schema, not a hand-written type that can fall out of sync.
The real challenges nobody mentions
1. The cache that lies
Turborepo's cache is powerful and sometimes too aggressive. If an environment variable changes but isn't declared in turbo.json's env, the cache isn't invalidated and you use an old build with the new config.
// turbo.json
{
"tasks": {
"build": {
"env": ["NODE_ENV", "NEXT_PUBLIC_API_URL", "EXPO_PUBLIC_API_URL"],
"outputs": [".next/**", "dist/**"]
}
}
}
Rule: any environment variable that affects the output must be declared explicitly.
2. Hot reload across packages
In development, when you modify a file in packages/domain, you want apps/web and apps/mobile to reflect it immediately without having to rebuild the package.
The solution: configure shared packages to export from source directly in development, not from the build.
// packages/domain/package.json
{
"exports": {
".": {
"development": "./src/index.ts",
"default": "./dist/index.js"
}
}
}
This requires the bundlers of each app to support direct TypeScript imports (Next.js does, Expo with Babel does).
3. The monorepo that abstracted too early
The mistake I've seen most frequently: turning everything into a shared package from day one, before knowing what actually needs to be shared.
The consequence: packages with a single consumer, abstractions that complicate more than they simplify, and an architecture that doesn't reflect the real domain boundaries.
Practical rule: something goes into packages/ when it has two confirmed consumers. Not before. Temporary duplication is preferable to premature abstraction in a context where boundaries aren't yet clear.
4. CI that takes just as long as without a monorepo
If CI isn't configured to leverage Turborepo's remote cache, every pipeline builds everything from scratch. The monorepo makes things worse because there's more to build.
# .github/workflows/ci.yml
- name: Setup Turborepo remote cache
uses: dtinth/setup-turbo-cache@v1
with:
token: ${{ secrets.TURBO_TOKEN }}
team: ${{ secrets.TURBO_TEAM }}
With remote cache, a PR that only touches the web app won't rebuild packages that didn't change.
Atomic Design in a monorepo: where the layers live
In the context of a monorepo with web and mobile, the question is: do atoms and molecules go in the shared package?
My answer after trying it both ways: no.
Atoms and molecules are the layer most coupled to the platform (styles, interactions, accessibility). Trying to share them creates components that aren't truly useful on either platform.
What works: each app has its own implementation of the UI layers (atoms, molecules, organisms). Shared code lives exclusively in packages/domain and packages/utils, which have no UI whatsoever.
Visual consistency between platforms is maintained through design tokens (colours, typography, spacing) shared as TypeScript constants, not as components.
What I'd do differently
Looking back, the changes that would make the most difference:
- Start with
strict: truefrom day zero — adding it later creates painful technical debt - Define domain boundaries before defining package structure — structure should reflect boundaries, not the other way around
- Set up remote cache before the first merge to main — not after builds already take 10 minutes
- Resist the temptation to share UI between platforms — the time-to-market of doing it right exceeds the benefit in most cases
When not to use a monorepo
To be honest: if you have a small team working on a single platform, a monorepo adds complexity without proportional benefit.
The inflection point where a monorepo starts paying for its overhead: two or more platforms sharing a domain, or several teams working on packages with interdependencies that need explicit coordination.
If you're not at that point, a well-organised repo with good conventions will take you further with less friction.
Architecture isn't the end. It's the means for the team to move fast with confidence, maintain what they've built, and change what needs changing without fear. A well-configured monorepo facilitates that. A poorly configured one gets in the way.
The difference is in the details nobody documents until they've paid for them in lost velocity.