{
  "id": "migrate-nextjs-14-to-16",
  "type": "prompts",
  "category": "prompts",
  "locale": "en",
  "url": "/prompts/migrate-nextjs-14-to-16",
  "title": "Prompt to Migrate a Next.js 14 App to Next.js 16",
  "description": "Structured AI agent prompt to migrate a Next.js 14 App Router project to Next.js 16 with breaking-change handling and incremental steps.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "TypeScript"
  ],
  "tags": [
    "nextjs",
    "typescript",
    "migrate"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "Use this prompt to drive a careful, incremental Next.js 14 to 16 migration — handling\nthe async `params`/`searchParams` API change, the React 19 peer dep, and deprecated\n`next/headers` patterns before upgrading.\n\n## Main Prompt\n\n```txt title=\"Main Prompt\"\nYou are migrating an existing Next.js 14 App Router project to Next.js 16.\n\nDo this in the following ordered steps — do not skip steps or combine them.\n\nStep 1 — Audit (read-only):\n  - List every file that uses `params` or `searchParams` as synchronous props.\n  - List every file that calls `cookies()`, `headers()`, or `draftMode()` without `await`.\n  - List every `next/image` usage with a deprecated prop (e.g., `layout`, `objectFit`).\n  - List the current `peerDependencies` for React and React DOM.\n  Output the audit as a plain list. Stop and wait for my review.\n\nStep 2 — Update deps:\n  - Run: `bun add next@16 react@19 react-dom@19 @types/react@19 @types/react-dom@19`\n  - Do NOT change any source files in this step.\n\nStep 3 — Fix async params:\n  - For every Page, Layout, and `generateMetadata` function that receives `params` or\n    `searchParams`, make the function `async` and `await` the prop before use.\n  - Type the props as `Promise<{ slug: string }>` (or the relevant shape), not `{ slug: string }`.\n\nStep 4 — Fix async dynamic APIs:\n  - Add `await` before every call to `cookies()`, `headers()`, and `draftMode()`.\n  - If the call is inside a non-async function, make that function async.\n\nStep 5 — Fix deprecated Image props:\n  - Remove `layout=\"fill\"` and replace with `fill` (boolean).\n  - Remove `objectFit` and `objectPosition` props and move them to a `className` or `style`.\n\nStep 6 — Verify:\n  - Run `bun run typecheck` and fix all remaining TypeScript errors.\n  - Run `bun run build` and confirm exit 0.\n\nAfter each step, show the diff and wait for confirmation before proceeding.\n```\n\n## Implementation Notes\n\n- In Next.js 15+, `params` and `searchParams` are `Promise`-wrapped. Accessing them synchronously\n  will compile but throw at runtime — the audit in Step 1 is essential before upgrading.\n- `cookies()` and `headers()` are async in Next.js 15+; the most common failure mode is calling\n  them inside a function that is not marked `async`.\n- React 19 removes some legacy APIs (`defaultProps` on function components, string refs). Run\n  `bun run typecheck` between each step to surface these early.\n\n## Expected File Changes\n\n```txt\npackage.json                          (edited — next, react, react-dom versions)\nbun.lock / package-lock.json          (edited automatically)\nsrc/app/**/page.tsx                   (edited — async params)\nsrc/app/**/layout.tsx                 (edited — async params)\nsrc/app/**/generateMetadata           (edited — async params)\nsrc/components/**/*.tsx               (edited — deprecated Image props)\n```\n\n## Acceptance Criteria\n\n- `bun run typecheck` exits 0 after all steps.\n- `bun run build` exits 0 with no warnings about deprecated APIs.\n- All dynamic routes render correctly in `bun run dev`.\n- No `params` or `searchParams` prop is accessed synchronously.\n\n## Test Commands\n\n```bash\nbun run typecheck\nbun run build\nbun run dev\n# visit several dynamic routes and confirm no runtime errors\ngrep -r \"layout=\\\"fill\\\"\" src/ && echo \"FAIL: deprecated Image prop found\"\ngrep -rn \"cookies()\\|headers()\\|draftMode()\" src/ | grep -v \"await\" && echo \"WARN: unawaited dynamic API\"\n```\n\n## Common AI Mistakes\n\n- Skipping the audit step and making all changes in one pass, causing hard-to-trace errors.\n- Typing async params as `{ params: { slug: string } }` instead of `{ params: Promise<{ slug: string }> }`.\n- Forgetting to upgrade `@types/react` alongside `react`, leaving type mismatches.\n- Not awaiting `cookies()` inside a nested helper function that itself is not async.\n\n## Fix Prompt\n\n```txt title=\"Fix Prompt\"\nThe build fails with type errors on params or a runtime error about cookies/headers.\nFix in order:\n1. For any page or layout receiving `params`, update the prop type to `Promise<{ slug: string }>` and\n   add `const { slug } = await params;` at the top of the function body.\n2. Find every call to `cookies()`, `headers()`, `draftMode()` not preceded by `await` and add it.\n   Make the containing function async if it isn't already.\n3. Run `bun run typecheck` and show me only the remaining errors.\n```"
}