Convert an existing project to a Turborepo monorepo. Use when the user wants to set up a monorepo, convert to Turborepo, or restructure a project with multiple apps/packages. Handles moving existing files, configuring workspaces, and preserving Git history.
This skill converts an existing project into a Turborepo monorepo structure. It handles:
apps/ directorypackages/project-root/
├── apps/
│ ├── web/ # Next.js frontend
│ ├── backend/ # Backend API (Motia, Express, etc.)
│ └── docs/ # Documentation site (optional)
├── packages/
│ ├── ui/ # Shared UI components
│ ├── config/ # Shared configs (ESLint, TypeScript)
│ └── types/ # Shared TypeScript types
├── turbo.json # Turborepo configuration
├── package.json # Root package.json with workspaces
└── pnpm-workspace.yaml # If using pnpm
# Create a backup branch
git checkout -b pre-monorepo-backup
git push origin pre-monorepo-backup
git checkout main
Create the directory structure first:
mkdir -p apps packages
Create a root package.json with workspaces:
{
"name": "project-monorepo",
"private": true,
"workspaces": [
"apps/*",
"packages/*"
],
"scripts": {
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"test": "turbo test",
"clean": "turbo clean"
},
"devDependencies": {
"turbo": "^2.3.3"
},
"packageManager": "[email protected]"
}
{
"$schema": "https://turbo.build/schema.json",
"ui": "tui",
"tasks": {
"build": {
"dependsOn": ["^build"],
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": [".next/**", "!.next/cache/**", "dist/**"]
},
"dev": {
"cache": false,
"persistent": true
},
"lint": {
"dependsOn": ["^lint"]
},
"test": {
"dependsOn": ["^build"]
},
"clean": {
"cache": false
}
}
}
For a Motia backend:
# Create apps/backend directory
mkdir -p apps/backend
# Move backend files (preserve structure)
mv src apps/backend/
mv motia.config.ts apps/backend/
mv python_modules apps/backend/
mv middlewares apps/backend/
mv requirements.txt apps/backend/
mv tsconfig.json apps/backend/
mv types.d.ts apps/backend/
Update apps/backend/package.json:
name to @project/backend# Move frontend to apps/web
mv frontend apps/web
Update apps/web/package.json:
name to @project/webMove common dependencies to root or create shared packages:
# Create shared config package
mkdir -p packages/config
Add monorepo-specific ignores:
# Turborepo
.turbo
# Node modules in all workspaces
node_modules
**/node_modules
# Build outputs
apps/**/dist
apps/**/.next
packages/**/dist
# Remove old node_modules
rm -rf node_modules
rm -rf apps/*/node_modules
rm -rf packages/*/node_modules
# Install from root
npm install
# Run build across all workspaces
npx turbo build
# Run dev servers
npx turbo dev
To preserve Git history when moving files:
# Use git mv instead of mv
git mv src apps/backend/src
git mv frontend apps/web
For already-moved files, Git will track renames automatically if the content similarity is high enough.
Solution: Ensure workspaces is correctly defined in root package.json and run npm install from root.
Solution: Update tsconfig.json in each app to use proper paths relative to new location.
Solution: Update .env file locations or use dotenv-cli to specify paths.
If something goes wrong:
git checkout pre-monorepo-backup
git branch -D main
git checkout -b main
git push --force origin main
turbo buildturbo dev