Manage multiple Next.js applications and shared packages efficiently using Turborepo and pnpm workspaces.
# Next.js Monorepo with Turborepo
Build scalable monorepos with Next.js and Turborepo using Google Antigravity IDE. This comprehensive guide covers workspace setup, shared packages, and build optimization.
## Why Turborepo?
Turborepo provides intelligent caching and parallel execution for monorepos. Google Antigravity IDE's Gemini 3 engine suggests optimal workspace configurations.
## Workspace Setup
```json
// turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["**/.env.*local"],
"globalEnv": ["NODE_ENV", "VERCEL_URL"],
"tasks": {
"build": {
"dependsOn": ["^build"],
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": [".next/**", "!.next/cache/**", "dist/**"]
},
"dev": {
"cache": false,
"persistent": true
},
"lint": {
"dependsOn": ["^lint"],
"outputs": []
},
"test": {
"dependsOn": ["^build"],
"inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts"]
},
"typecheck": {
"dependsOn": ["^typecheck"],
"outputs": []
}
}
}
```
```json
// package.json (root)
{
"name": "antigravity-monorepo",
"private": true,
"workspaces": ["apps/*", "packages/*"],
"scripts": {
"dev": "turbo dev",
"build": "turbo build",
"lint": "turbo lint",
"test": "turbo test",
"typecheck": "turbo typecheck",
"clean": "turbo clean && rm -rf node_modules"
},
"devDependencies": {
"turbo": "^2.0.0",
"typescript": "^5.0.0"
},
"packageManager": "pnpm@8.15.0"
}
```
## Shared UI Package
```typescript
// packages/ui/src/Button.tsx
import { forwardRef, ButtonHTMLAttributes } from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "./utils";
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md font-medium transition-colors",
{
variants: {
variant: {
primary: "bg-blue-600 text-white hover:bg-blue-700",
secondary: "bg-gray-100 text-gray-900 hover:bg-gray-200",
outline: "border border-gray-300 hover:bg-gray-100",
},
size: {
sm: "h-8 px-3 text-sm",
md: "h-10 px-4",
lg: "h-12 px-6 text-lg",
},
},
defaultVariants: {
variant: "primary",
size: "md",
},
}
);
interface ButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {}
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, ...props }, ref) => (
<button
ref={ref}
className={cn(buttonVariants({ variant, size }), className)}
{...props}
/>
)
);
Button.displayName = "Button";
```
```json
// packages/ui/package.json
{
"name": "@repo/ui",
"version": "0.0.0",
"private": true,
"exports": {
"./button": "./src/Button.tsx",
"./card": "./src/Card.tsx",
"./input": "./src/Input.tsx",
".": "./src/index.ts"
},
"dependencies": {
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"tailwind-merge": "^2.0.0"
},
"devDependencies": {
"@repo/typescript-config": "workspace:*",
"react": "^18.2.0",
"typescript": "^5.0.0"
}
}
```
## Shared Config Packages
```typescript
// packages/eslint-config/next.js
module.exports = {
extends: [
"next/core-web-vitals",
"turbo",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
"@typescript-eslint/explicit-function-return-type": "off",
},
};
// packages/typescript-config/base.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"declaration": true,
"declarationMap": true
}
}
```
## App Configuration
```typescript
// apps/web/next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
transpilePackages: ["@repo/ui"],
experimental: {
optimizePackageImports: ["@repo/ui"],
},
};
export default nextConfig;
```
```typescript
// apps/web/src/app/page.tsx
import { Button } from "@repo/ui/button";
import { Card } from "@repo/ui/card";
export default function HomePage() {
return (
<main>
<Card>
<h1>Welcome to Antigravity</h1>
<Button variant="primary">Get Started</Button>
</Card>
</main>
);
}
```
## Best Practices
- Use workspace protocol for internal dependencies
- Configure proper transpilePackages in Next.js
- Apply task caching for faster builds
- Share ESLint and TypeScript configs
- Use remote caching for CI/CD
- Keep packages focused and reusable
Google Antigravity IDE provides monorepo templates and automatically configures optimal Turborepo settings for your workspace.This Next.js prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their next.js implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Next.js projects, consider mentioning your framework version, coding style, and any specific libraries you're using.