Overview
This lesson details the process of migrating an existing, standalone Next.js application into your newly created Turborepo monorepo structure. We'll cover moving files, updating configurations, and integrating it as a workspace.
Key Points
-
Move Next.js App: Physically relocate your existing Next.js project directory into the
apps/folder of your monorepo (e.g.,my-monorepo/apps/my-next-app). -
Update
package.json: Adjust thenamefield in the Next.js app'spackage.jsonto be unique within the monorepo. Ensure its dependencies are properly listed. -
Configure
tsconfig.json: Update thetsconfig.jsonwithin your Next.js app to correctly resolve paths to shared packages. You might usebaseUrlandpathsfor this. -
Adjust Build Scripts in
turbo.json: Add or modify thebuild,dev,lint, andtestscripts for your Next.js app within the rootturbo.jsonto leverage Turborepo's task runner. -
Install Dependencies: Run
pnpm install(or your chosen package manager's equivalent) from the monorepo root to link all workspace dependencies.
Quick Example
Here's how you might define a build script for a Next.js app named web in turbo.json:
json{ "pipeline": { "build": { "dependsOn": ```json ["^build"]
, "outputs":
json[".next/**", "!**/.next/cache/**"]
},
"web#build": {
"dependsOn":
json["^build"]
, "outputs":
json[".next/**", "!**/.next/cache/**"]
}
}
}
In `apps/web/package.json` you would have a `build` script like `next build`.
## Summary
Migrating a Next.js app to a Turborepo involves relocating files, updating individual `package.json` and `tsconfig.json` files, and integrating its build process into the monorepo's `turbo.json`. This ensures the app can be built and developed efficiently within the new structure.