How to Add Next.js to an Existing React Project

Add Next.js to an existing React app by installing the framework, adding scripts, and creating an app directory.

6 min read

To add Next.js to an existing React project, install the framework package, add the dev and build scripts, and create an app directory with a root layout and a home page. Your React components stay as they are and move into the new structure one page at a time. The process takes a few minutes and keeps every component you already wrote.

Start by installing the packages:

bashbash
npm install next@latest react@latest react-dom@latest

Next.js is a package like any other, so it lands in node_modules next to react and react-dom. The App Router bundles its own React build internally, but you still declare those two packages so your editor and tooling know the versions. Add these scripts to your package.json:

jsonjson
"scripts": {
  "dev": "next dev",
  "build": "next build",
  "start": "next start"
}

The dev script starts the development server, build produces an optimized production bundle, and start serves that bundle. Replace any scripts left over from a previous bundler, because Next.js manages compilation itself.

Create the app directory

Next.js reads routes from an app folder, so create that folder at the project root. Inside it, two files are enough to get a working page: a root layout and a home page. The folder name matters, because the router treats app as the root of every route.

App.tsxApp.tsx
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

The root layout is required and must contain the html and body tags, because it is the outer shell of every page in the app.

App.tsxApp.tsx
// app/page.tsx
export default function HomePage() {
  return <h1>Hello from Next.js</h1>;
}

This page is served at the root URL, /. Add more folders and page files the same way, and each one becomes a route at its folder's path.

Run the server

bashbash
npm run dev

Open http://localhost:3000 and the home page appears. Move your existing React components into the app directory, importing them from their new locations, and each page file becomes a route.

Most of your component code stays exactly the same. What changes is the shell around it: the old HTML entry point, the bundler config, and any client-side router are replaced by Next.js, so you can delete those files after the move.

The most common mistake is running the old dev command instead of npm run dev. If the page still looks like the old bundler output, check that the scripts in package.json point to next and that the app folder exists at the project root.

Add TypeScript

If the project is still JavaScript, rename one file to a TypeScript extension and restart the server. Next.js installs TypeScript and generates a tsconfig.json on the next run, so no manual configuration is required. This step is optional, and plain JavaScript projects work as they are.

bashbash
mv app/page.js app/page.tsx
npm run dev

Components written in plain JavaScript still work alongside TypeScript files, so you can convert the project gradually.

If you are coming from a bundler-based setup, the move is similar but has a codemod to help. See Migrating from Vite to Next.js or Migrating from Create React App to Next.js.

Rune AI

Rune AI

Key Insights

  • Install next, react, and react-dom to add the framework.
  • Add dev, build, and start scripts to package.json.
  • Create an app directory with a root layout and a page.
  • Start the dev server and visit localhost:3000.
  • TypeScript support is added by renaming a file to .tsx.
RunePowered by Rune AI

Frequently Asked Questions

Do I have to rewrite my React components?

No. Your existing React components keep working. Move them into the app directory, and Next.js renders them as pages or shared components.

Does my React version matter?

Install the latest react and react-dom. The App Router uses its own React canary build internally, but you still declare those packages for tooling.

Will my Vite or CRA config carry over?

Mostly not. Next.js brings its own bundler and config, so you can delete the old bundler config after the move. Codemods exist for Vite and Create React App migrations.

Conclusion

Adding Next.js to an existing React project is mostly a matter of installing the package, adding scripts, and creating an app directory with a root layout and a home page. Your components move over as they are.