Skip to main content

Lesson 7 of 11

Turborepo Monorepo - Expo + Shared Code

A monorepo hosts multiple packages in one git repository. The killer benefit for mobile: share TypeScript types, validation schemas (Zod), and utility functions between your React Native app and your Next.js web app or Node.js backend - with a single source of truth.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Turborepo + Expo Architecture

Turborepo + Expo Architecture
# Create monorepo
npm install -g turbo
npx create-turbo@latest my-monorepo --package-manager npm
cd my-monorepo

# Folder structure:
# my-monorepo/
# ├── apps/
# │   ├── mobile/         ← Expo app (React Native)
# │   ├── web/            ← Next.js app
# │   └── api/            ← Express / Fastify backend
# ├── packages/
# │   ├── shared/         ← shared types, utils, Zod schemas
# │   ├── ui/             ← shared components (for web)
# │   └── config/         ← shared eslint, tsconfig, prettier
# └── turbo.json

# turbo.json - pipeline configuration
{
  "$schema": "https://turbo.build/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],   // build deps first
      "outputs": [".expo/**", ".next/**", "dist/**"]
    },
    "test": {
      "dependsOn": ["^build"],
      "outputs": ["coverage/**"]
    },
    "lint": { "outputs": [] },
    "dev": { "cache": false, "persistent": true }
  }
}

# Root package.json scripts
{
  "scripts": {
    "dev": "turbo run dev",
    "build": "turbo run build",
    "test": "turbo run test",
    "lint": "turbo run lint"
  }
}

Shared Package - Types and Validation

Shared Package - Types and Validation
# packages/shared/
# ├── src/
# │   ├── types/
# │   │   ├── user.ts
# │   │   ├── product.ts
# │   │   └── index.ts
# │   ├── schemas/
# │   │   ├── auth.ts      ← Zod schemas shared between app and API
# │   │   └── index.ts
# │   ├── utils/
# │   │   ├── formatCurrency.ts
# │   │   ├── formatDate.ts
# │   │   └── index.ts
# │   └── index.ts
# └── package.json

# packages/shared/package.json
{
  "name": "@myapp/shared",
  "version": "0.0.1",
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "scripts": { "lint": "eslint src --ext .ts" },
  "dependencies": { "zod": "^3.23.0" }
}

// packages/shared/src/schemas/auth.ts
import { z } from 'zod';

export const signUpSchema = z.object({
  name: z.string().min(2, 'Name must be at least 2 characters'),
  email: z.string().email('Invalid email'),
  password: z.string().min(8, 'Min 8 characters').regex(/[A-Z]/, 'Needs uppercase').regex(/[0-9]/, 'Needs a number'),
});

export type SignUpInput = z.infer<typeof signUpSchema>;

// ---- Use in mobile app ----
// apps/mobile/package.json → "dependencies": { "@myapp/shared": "*" }
import { signUpSchema, type SignUpInput } from '@myapp/shared';
// Same Zod schema - no duplication between mobile and API

// ---- Use in API server ----
// apps/api/src/routes/auth.ts
import { signUpSchema } from '@myapp/shared';
router.post('/signup', (req, res) => {
  const result = signUpSchema.safeParse(req.body);
  if (!result.success) return res.status(400).json(result.error.format());
  // proceed with validated data
});

Running the Monorepo

Running the Monorepo
# Install all packages from root
npm install

# Dev - all apps in parallel with caching
turbo dev

# Run specific app only
turbo dev --filter mobile
turbo dev --filter web

# Build with caching (unchanged packages skip build)
turbo build

# Run tests across all packages in parallel
turbo test

# Add a dependency to a specific workspace
npm install dayjs --workspace=apps/mobile

# Turborepo cache:
# Second run of unchanged packages: < 1 second (from cache)
# First run: full build
# Remote cache (turbo.build): share cache on CI - reduce CI time by 80%

# Enable remote cache:
turbo login   # authenticates to turbo.build
turbo link    # links monorepo to your Turborepo account
# Now: CI builds reuse cached outputs from developers' machines

Tip

Tip

Practice Turborepo Monorepo Expo Shared Code in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Sharing UI components, business logic, and API clients across Web and Mobile

Practice Task

Note

Practice Task - (1) Write a working example of Turborepo Monorepo Expo Shared Code from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Turborepo Monorepo Expo Shared Code is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/11 done