Upgrading and the codemods

Intermediate · 10 min

Move a Next.js app to version 16 with next upgrade and the official codemods, know the breaking changes the codemods miss, and verify with a checklist.

Before thisBuild output and next build

IQ CODING HOUSE · 10 min

Upgrading and the codemods

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by IQ CODING HOUSEReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • next upgrade (16.1 and later) or npx @next/codemod@canary upgrade latest updates Next.js and React and applies the recommended codemods; commit your work first, because codemods refuse to run on a dirty git tree.
  • A codemod is a scripted source rewrite; run it with --dry or --print first, review the diff, and resolve any @next-codemod-error comments it leaves.
  • Version 16 raises the floor to Node.js 20.9 and TypeScript 5.1, makes Turbopack the default bundler, and removes synchronous access to params, searchParams, cookies(), headers(), and draftMode().
  • The upgrade codemod does not run every migration: the async request API rewrite is its own codemod, next-async-request-api.
  • middleware.ts becomes proxy.ts, next lint is removed, and several next/image defaults changed, so a green build is not the end of an upgrade.

Why codemods exist

A framework that changes its API has two choices: break your code, or give you a tool that fixes it. Next.js does the second where it can. A codemod is a script that parses your source into a syntax tree, rewrites specific patterns, and writes the files back. It is a find-and-replace that understands code, which is why it can turn cookies().get('a') into (await cookies()).get('a') and not merely match text.

Codemods cover the mechanical changes. The judgment calls, such as whether your webpack plugin has a Turbopack equivalent, stay with you.

The upgrade command

From Next.js 16.1, the CLI has a command for the whole move:

npx next upgrade

Before 16.1 it does not exist, so the docs say to use the standalone package, which also takes a target:

npx @next/codemod@canary upgrade latest

The tool accepts a revision as patch, minor, major, a dist tag such as latest or canary, or an exact version like 16. Flags worth knowing are --skip-react-upgrade and --skip-eslint-upgrade to leave those packages alone, and -y to accept every default; it is also assumed in a non-interactive shell such as CI. next upgrade --agent hands the migration guides and codemod command to a coding agent. The docs mark that as experimental.

If you would rather do it by hand, install the latest packages:

npm i next@latest react@latest react-dom@latest eslint-config-next@latest

The docs ship a version guide for each of 14, 15, and 16. If you are more than one major behind, reading the intermediate guides is the safe route.

Running a codemod

The general form from the docs:

npx @next/codemod <transform> <path>

with --dry to change nothing and --print to show the result for comparison.

I tried two on a scratch Next.js 14.2 project. The first run stopped:

Thank you for using @next/codemod!

But before we continue, please stash or commit your git changes.

You may use the --force flag to override this safety check.

The check works the way the message says: not a git repo or a dirty tree stops it, until you commit or pass --force. Treat that as a feature, because the diff afterwards is your review.

With the project committed, next-async-request-api turned this:

import { cookies } from 'next/headers'
export default function Page({ params }: { params: { id: string } }) {
  const c = cookies().get('a')
  return <p>{params.id}{c?.value}</p>
}

into this:

import { cookies } from 'next/headers'
export default async function Page(props: { params: Promise<{ id: string }> }) {
  const params = await props.params;
  const c = (await cookies()).get('a')
  return <p>{params.id}{c?.value}</p>
}

and middleware-to-proxy renamed middleware.ts to proxy.ts and the exported function to proxy. The docs add that it also renames related config properties such as skipMiddlewareUrlNormalize to skipProxyUrlNormalize. When the async rewrite cannot be sure, it leaves an @next-codemod-error comment and may add a temporary UnsafeUnwrapped cast. Those mean the migration is unfinished; resolve them before moving on. See dynamic APIs for the end state.

What changed in 16

The version 16 guide is long. Grouped by how much attention they need:

Platform floor. Node.js 20.9 or newer, TypeScript 5.1 or newer, and browsers Chrome 111, Edge 111, Firefox 111, Safari 16.4. Check your CI image and hosting runtime first. Self-hosting and TypeScript in Next.js cover those sides.

Bundler. Turbopack is the default for dev and build. If you have a custom webpack config, a build fails unless you pick: --turbopack to build with Turbopack and ignore the webpack config, migrate the config to Turbopack options, or --webpack to opt out. See Turbopack and the dev server.

Async request APIs. cookies, headers, draftMode, params, and searchParams are async only. Version 15 allowed synchronous access temporarily and 16 removed it. The upgrade codemod does not necessarily run this rewrite, so run npx @next/codemod@canary next-async-request-api . yourself. npx next typegen generates PageProps, LayoutProps, and RouteContext type helpers.

Renames and removals.

  • middleware is now proxy. The docs warn that edge middleware should not be blindly renamed, because proxy runs on Node.js.
  • next lint is gone, and next build no longer lints. The next-lint-to-eslint-cli codemod migrates to the ESLint CLI.
  • unstable_ prefixes are dropped from stabilized APIs, and the experimental_ppr segment config is removed. Both have codemods.
  • AMP support and runtime configuration (serverRuntimeConfig and publicRuntimeConfig) are removed.

next/image defaults. The guide marks several as breaking: local images with query strings, the minimumCacheTTL, imageSizes, and qualities defaults, a local-IP restriction, and a maximum redirects limit. Check any page whose images look different or return an error. See optimizing images.

Parallel routes. Every slot now needs a default.js. See parallel and intercepting routes.

Caching. The caching functions have new or changed signatures, updateTag and refresh are new, and the guide says not to enable React Compiler or Cache Components merely to finish the upgrade. You can upgrade first and adopt Cache Components later.

Verifying

The guide ends with a checklist. Its useful items: confirm Node, TypeScript, and React meet the floor; make sure dev and build scripts agree about the bundler; confirm proxy or middleware changes kept the runtime and routing you need; check images against the new defaults; confirm CI no longer calls next lint; and run the app’s own checks. It states that a green build does not close runtime findings. After the upgrade, run next build and compare the route table with the one from before: a route that flipped from ○ to ƒ is a regression the compiler will not report.

What the video covers

The video is titled “How to Upgrade to Next js 16 Without Breaking Your App”. Its description says it walks through preparing a project, breaking changes to fix first, new features and performance changes, updating next.config.js, package updates, debugging common upgrade errors, and a final checklist, for both routers. There are no chapters, and transcripts were unavailable, so I cannot say how well it handles any of those points. I could not check the narration either, so check by ear that it is clear English before relying on it.

This is the weakest pick in the group. It is the only recent video sourced that targets 16 specifically, from a small channel, and the description reads as generic. The runner-up, Wisp Blog’s 4:09 video on the 14 to 15 codemod, is accurate for its version but covers a different upgrade. The docs and the checklist above are the primary material here.

Where this fits

You can now keep an app current. The last lesson goes the other way: the Pages Router for legacy apps, for the apps that did not start on the App Router.

Check yourself

  1. 01What is the difference between next upgrade and running one named codemod?

    next upgrade moves the whole app to a target version: it updates next, react, and react-dom, enables Turbopack, and offers the recommended codemods. A named codemod, run as npx @next/codemod <transform> <path>, applies one specific source rewrite, such as middleware-to-proxy.

  2. 02Why does the codemod tool want a clean git state?

    It rewrites your files in place. Requiring a stash or commit first means you can review and revert exactly what it changed. I confirmed it refuses to run otherwise, and --force overrides the check.

  3. 03After upgrading to 16, your build fails on params.id. What happened and what fixes it?

    Next.js 15 allowed synchronous access to params with a warning; 16 removed it, so params and searchParams must be awaited. The next-async-request-api codemod rewrites the components and typegen can generate PageProps helper types.

  4. 04Name two things in the Next.js 16 upgrade that need checking by hand rather than trusting the codemod?

    Any custom webpack configuration, since Turbopack is now the default and the build fails when it finds one unless you pass --turbopack or --webpack, and middleware that relied on the edge runtime, since proxy runs on Node.js and the docs say not to rename edge middleware blindly. Image defaults and next lint usage in scripts and CI are also on the checklist.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark