Codemods
Codemods are transformations that run on your codebase programmatically. This allows a large number of changes to be programmatically applied without having to manually go through every file.
Next.js provides Codemod transformations to help upgrade your Next.js codebase when an API is updated or deprecated.
Usage
In your terminal, navigate (cd) into your project's folder, then run:
Replacing <transform> and <path> with appropriate values.
transform- name of transformpath- files or directory to transform--dryDo a dry-run, no code will be edited--printPrints the changed output for comparison
Next.js Codemods
13.2
Use Built-in Font
built-in-next-font
This codemod uninstalls the @next/font package and transforms @next/font imports into the built-in next/font.
For example:
Transforms into:
13.0
Rename Next Image Imports
next-image-to-legacy-image
Safely renames next/image imports in existing Next.js 10, 11, or 12 applications to next/legacy/image in Next.js 13. Also renames next/future/image to next/image.
For example:
Transforms into:
Migrate to the New Image Component
next-image-experimental
Dangerously migrates from next/legacy/image to the new next/image by adding inline styles and removing unused props.
- Removes
layoutprop and addsstyle. - Removes
objectFitprop and addsstyle. - Removes
objectPositionprop and addsstyle. - Removes
lazyBoundaryprop. - Removes
lazyRootprop.
Remove <a> Tags From Link Components
new-link
Remove <a> tags inside Link Components, or add a legacyBehavior prop to Links that cannot be auto-fixed.
For example:
In cases where auto-fixing can't be applied, the legacyBehavior prop is added. This allows your app to keep functioning using the old behavior for that particular link.
11
Migrate from CRA
cra-to-next
Migrates a Create React App project to Next.js; creating a Pages Router and necessary config to match behavior. Client-side only rendering is leveraged initially to prevent breaking compatibility due to window usage during SSR and can be enabled seamlessly to allow the gradual adoption of Next.js specific features.
Please share any feedback related to this transform in this discussion.
10
Add React imports
add-missing-react-import
Transforms files that do not import React to include the import in order for the new React JSX transform to work.
For example:
Transforms into:
9
Transform Anonymous Components into Named Components
name-default-component
Versions 9 and above.
Transforms anonymous components into named components to make sure they work with Fast Refresh.
For example:
Transforms into:
The component will have a camel-cased name based on the name of the file, and it also works with arrow functions.
8
Transform AMP HOC into page config
withamp-to-config
Transforms the withAmp HOC into Next.js 9 page configuration.
For example:
6
Use withRouter
url-to-withrouter
Transforms the deprecated automatically injected url property on top level pages to using withRouter and the router property it injects. Read more here: https://nextjs.org/docs/messages/url-deprecated
For example:
This is one case. All the cases that are transformed (and tested) can be found in the __testfixtures__ directory.
Last updated