vercel/next.js
next-codemod
Purpose
@next/codemod ships the jscodeshift-based transforms that upgrade user code between Next.js major versions. It is what next upgrade runs under the hood, and is also installable directly as a CLI: npx @next/codemod <transform> <path>.
Source: packages/next-codemod/.
Directory layout
packages/next-codemod/
├── README.md
├── bin/ # CLI entry
├── lib/ # Shared helpers (parsing, file walking)
├── transforms/ # One directory per transform
├── scripts/ # Maintenance scripts
├── package.json
└── turbo.jsonEach subdirectory of transforms/ is one named codemod: a transform script plus its test fixtures. jscodeshift visits each file in the user's project, parses it to an AST, runs the transform, and writes back the result.
Examples of transforms
The catalog grows with each release. Examples that have shipped:
name-default-component— gives anonymous default-exported page components a stable namenew-link— converts old<Link><a/></Link>patterns to the modern<Link>shapenext-image-to-legacy-image— moves users offnext/imageontonext/legacy/imagewhere applicablenext-image-experimental— opts code into the (now-default)next/imageapp-dir-runtime-config-experimental— migrates runtime config to the App Router shapebuilt-in-next-font— rewrites third-party@next/fontimports to the built-innext/fontcra-to-next— migrates Create React App projects to Next.jsmetadata-to-viewport-export— splitsviewportout ofmetadataper the App Router schema
The transform README lists the canonical set; new entries are added as the framework deprecates APIs.
How next upgrade invokes it
packages/next/src/cli/next-upgrade.ts shells out to npx @next/codemod with the right transform name when you run next upgrade. The upgrade flow:
graph LR
User[next upgrade] --> CLI[cli/next-upgrade.ts]
CLI --> Detect[detect installed Next.js version]
Detect --> Plan[plan upgrade path]
Plan --> Bump[bump package.json]
Bump --> Run[run codemods for each version step]
Run --> Codemod[@next/codemod transforms]Adding a new codemod
To add a transform:
- Create
packages/next-codemod/transforms/<name>/withindex.ts(the jscodeshift visitor) and__testfixtures__/(input/output pairs). - Add an entry to the codemod README and the
next upgradeplan. - Ship a release.
Tests are run with jscodeshift --runInBand inside the package's own test runner.
Key source files
| File | Purpose |
|---|---|
packages/next-codemod/bin/ |
CLI entry script |
packages/next-codemod/lib/ |
Shared helpers (file walkers, parsers) |
packages/next-codemod/transforms/<name>/index.ts |
One transform per directory |
packages/next/src/cli/next-upgrade.ts |
The framework-side next upgrade driver |
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.