Next.js Codemods

Next.js provides Codemod transformations to help upgrade your Next.js codebase when a feature is deprecated.

Codemods are transformations that run on your codebase programmatically. This allows for a large amount of changes to be applied without having to manually go through every file.

Usage

npx @next/codemod <transform> <path>

  • transform - name of transform, see available transforms below.
  • path - files or directory to transform
  • --dry Do a dry-run, no code will be edited
  • --print Prints the changed output for comparison

Next.js 11

cra-to-next (experimental)

Migrates a Create React App project to Next.js; creating a pages directory 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 gradual adoption of Next.js specific features.

Please share any feedback related to this transform in this discussion.

Next.js 10

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:

  1. // my-component.js
  2. export default class Home extends React.Component {
  3. render() {
  4. return <div>Hello World</div>
  5. }
  6. }

Transforms into:

  1. // my-component.js
  2. import React from 'react'
  3. export default class Home extends React.Component {
  4. render() {
  5. return <div>Hello World</div>
  6. }
  7. }

Next.js 9

name-default-component

Transforms anonymous components into named components to make sure they work with Fast Refresh.

For example:

  1. // my-component.js
  2. export default function () {
  3. return <div>Hello World</div>
  4. }

Transforms into:

  1. // my-component.js
  2. export default function MyComponent() {
  3. return <div>Hello World</div>
  4. }

The component will have a camel cased name based on the name of the file, and it also works with arrow functions.

Usage

Go to your project

  1. cd path-to-your-project/

Run the codemod:

  1. npx @next/codemod name-default-component

withamp-to-config

Transforms the withAmp HOC into Next.js 9 page configuration.

For example:

  1. // Before
  2. import { withAmp } from 'next/amp'
  3. function Home() {
  4. return <h1>My AMP Page</h1>
  5. }
  6. export default withAmp(Home)
  1. // After
  2. export default function Home() {
  3. return <h1>My AMP Page</h1>
  4. }
  5. export const config = {
  6. amp: true,
  7. }

Usage

Go to your project

  1. cd path-to-your-project/

Run the codemod:

  1. npx @next/codemod withamp-to-config

Next.js 6

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:

  1. // From
  2. import React from 'react'
  3. export default class extends React.Component {
  4. render() {
  5. const { pathname } = this.props.url
  6. return <div>Current pathname: {pathname}</div>
  7. }
  8. }
  1. // To
  2. import React from 'react'
  3. import { withRouter } from 'next/router'
  4. export default withRouter(
  5. class extends React.Component {
  6. render() {
  7. const { pathname } = this.props.router
  8. return <div>Current pathname: {pathname}</div>
  9. }
  10. }
  11. )

This is one case. All the cases that are transformed (and tested) can be found in the __testfixtures__ directory.

Usage

Go to your project

  1. cd path-to-your-project/

Run the codemod:

  1. npx @next/codemod url-to-withrouter