How to Migrate from Create React App to Next JS ?
Last Updated :
11 Sep, 2024
Migrating from Create React App to Next.js is a structured process that includes setting up a Next.js project, reorganizing your files, updating dependencies, and utilizing Next.js features for better performance and server-side rendering.
Prerequisites:
Approach
To migrate from create react app to NextJS first create the project using create-react-app, then uninstall the react-script and add nextjs as dependency. Update the package.json file along with the project structure similar to next project.
Steps to Migrate from create react app to NextJS
Step 1: Initialize React Project using CRA command
Open the directory where you want to create your react app and then open your command line or PowerShell/terminal and run the following command to create-react-app.
npx create-react-app react-to-next
cd react-to-next
npm start
Step 2: Remove the React Script from dependencies
Uninstall the dependencies. Now we have to uninstall the dependencies which are react-scripts.
npm uninstall react-scripts
Step 3: Install NextJS as Dependency
Now install the Next package or dependency. Run the following command to install next.
npm i next
Step 4: Update scripts in package.json
Change the scripts of the package.json. Copy and paste the following script into your package.json file.
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
Step 5: Update the Project Structure
Create a pages folder in your root directory. After creating your folder named as pages create a javascript file named index.js inside the pages folder.
Project Structure:
Directory Example: This example demonstrate creating Next JS application.
JavaScript
// Filename - pages/index.js
import Image from "next/image";
import logo from "../src/logo.svg";
function App() {
return (
<div className="App">
<header className="App-header">
<Image
src={logo}
className="App-logo"
width={200}
height={200}
alt="logo"
/>
<p>
Edit <code>src/App.js</code> and save to
reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
Step to run the application: Run the App by the following command:
npm run dev
Output: This output will be visble on http://localhost:3000/ on the browser window.
Summary
Migration to NextJS starts by creating project with Create React App. Then, uninstall react-scripts and add Next.js as a dependency. Update the package.json file and restructure your project to align with the Next.js format.
Similar Reads
How to Migrate from create-react-app to Vite? In this article, we will learn how we can migrate from create-react-app to Vite. Vite is a build tool for frontend development that aims for fast and efficient development. It provides a development server with hot module replacement and supports various JavaScript flavors, including JSX, out of the
3 min read
Migrating from Create React App to NextJS: A Practical Guide Migrating from Create React App (CRA) to Next.js involves transitioning from a client-side rendered React application to a framework that offers server-side rendering, static site generation, and built-in routing capabilities. This guide provides a step-by-step approach to help you effectively migra
4 min read
How to Create a Next.js form ? Forms are an essential part of many web applications, allowing users to input data and interact with the website. Next.js, with its powerful features for building server-rendered React applications, provides an ideal platform for creating forms. In this article, we'll explore step-by-step how to cre
4 min read
Next.js Migrating from React Router Are you using React Router in your web application and planning to switch to Next.js? Don't worry, the migration process is easy and we've got you covered! This blog post will guide you through the process and help you understand the benefits of doing so. What is Next.js? Next.js is a React-based fr
5 min read
How to Create an NPM Library from React Components ? Creating an NPM (Node Package Manager) library from your React components is a great way to make them reusable across different projects. This approach allows you to easily reuse and update components without needing to rewrite them for every new project.Steps to Create an NPM Library from a React C
4 min read
How to Create a New Next JS 13+ App? Creating a new Next.js 13+ application is a straightforward process that sets up a modern React-based framework with built-in features like server-side rendering, static site generation, and API routes. Next JS streamlines and automates the setup of essential React tooling, handling tasks such as bu
2 min read
How To Convert From .ejs Template to React? Converting from Embedded JavaScript templates (.ejs) to React can significantly enhance the flexibility and interactivity of your web applications. Reactâs component-based architecture provides a more dynamic way to build user interfaces compared to traditional templating engines like EJS. This arti
4 min read
How To Create a Website in ReactJS? ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS.PrerequisitesNPM & Node.jsR
5 min read
How are forms created in ReactJS ? Form is a document that stores information of a user on a web server using interactive controls. A form contains different kinds of information such as username, password, contact number, email ID, etc. Creating a form in React is almost similar to that of HTML if we keep it simple and just make a s
3 min read
How to change port in Next.js App Next.js provides the flexibility to change the default port of the development server using command-line options such as -p <port_number> or by updating the "scripts" section in the package.json file, offering developers multiple ways to customize the port configuration based on their requirem
3 min read