Modern React development has evolved significantly. Many projects that were initially created using Create React App (CRA) are now migrating to Vite for faster builds, improved developer experience, and fewer dependency issues.
If your project uses react-scripts and you’re facing slow startup, build issues, or vulnerability warnings, migrating to Vite can be a great improvement.
This guide explains how to migrate a React project from Create React App to Vite, including all common pitfalls such as environment variables, public assets, scripts, and configuration changes.
Why Move from Create React App to Vite?
Create React App was once the standard way to start a React project. However, it has several limitations today:
- Slow development server startup
- Large dependency tree
- Frequent
npm auditvulnerabilities - Webpack-based builds that are slower than modern bundlers
- Limited customization without ejecting
Vite solves these problems by using ESBuild for development and Rollup for production builds.
Key benefits include:
- Instant dev server startup
- Faster hot module replacement (HMR)
- Smaller dependency tree
- Simpler configuration
- Modern tooling
Step 1: Remove Create React App Dependencies
CRA projects depend on react-scripts, which powers the development and build processes.
Remove it from your project:
npm uninstall react-scripts
You may also remove testing libraries if you plan to configure testing later:
npm uninstall @testing-library/jest-dom @testing-library/react @testing-library/user-event
Step 2: Install Vite and React Plugin
Install Vite and the React plugin required to support JSX and React Fast Refresh.
npm install -D vite @vitejs/plugin-react
If your project uses TypeScript:
npm install -D typescript
Step 3: Update Package Scripts
In CRA projects, scripts look like this:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Replace them with Vite scripts:
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
Now the development server will run using:
npm run dev
Step 4: Create the Vite Configuration
Create a file named vite.config.ts in the project root.
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()]
})
This enables React support and hot module reloading.
Step 5: Move index.html to the Root Folder
In Create React App, index.html is located inside the public folder.
public/index.html
In Vite, index.html must be placed in the project root.
project
├─ index.html
├─ src
└─ public
Step 6: Replace index.tsx with main.tsx
CRA uses:
src/index.tsx
Vite typically uses:
src/main.tsx
Rename the file and update the content:
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Step 7: Fix PUBLIC_URL Assets
One of the most common migration issues is the use of:
%PUBLIC_URL%
Example from CRA:
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
Vite does not support %PUBLIC_URL%.
Instead, place assets inside the public folder and reference them directly:
public/
├─ favicon.ico
├─ logo192.png
└─ manifest.json
Then update the HTML:
<link rel="icon" href="/favicon.ico" />
<link rel="manifest" href="/manifest.json" />
<link rel="apple-touch-icon" href="/logo192.png" />
Step 8: Update Environment Variables
CRA environment variables use the prefix:
REACT_APP_
Example:
REACT_APP_API_URL
In Vite, environment variables must start with:
VITE_
Example:
VITE_API_URL
Access them using:
import.meta.env.VITE_API_URL
Step 9: Clean Install Dependencies
After making configuration changes, perform a clean install.
rm -rf node_modules
rm package-lock.json
npm install
Then start the dev server:
npm run dev
The app will run on:
http://localhost:5173
Step 10: Fix React Version Compatibility
Create React App works best with React 18. If your project contains an unsupported version, update it:
"react": "^18.2.0",
"react-dom": "^18.2.0"
Then reinstall dependencies.
Common Issues During Migration
Blank Page in Browser
Check the browser console for errors and verify that:
-
index.htmlis in the root folder - the script path is correct
<script type="module" src="/src/main.tsx"></script>
Static Files Not Loading
Ensure assets are inside the public folder and referenced with /.
Incorrect:
%PUBLIC_URL%/image.png
Correct:
/image.png
Environment Variables Not Working
Make sure variables use the correct prefix.
Incorrect:
process.env.REACT_APP_API
Correct:
import.meta.env.VITE_API
Recommended Folder Structure
project
├─ public
│ ├─ favicon.ico
│ └─ manifest.json
│
├─ src
│ ├─ components
│ ├─ pages
│ ├─ App.tsx
│ └─ main.tsx
│
├─ index.html
├─ package.json
└─ vite.config.ts
Final Thoughts
Migrating from Create React App to Vite can dramatically improve development speed and simplify project maintenance. The process mainly involves replacing build tools, updating environment variables, fixing public assets, and adjusting the project structure.
Once migrated, developers benefit from faster builds, modern tooling, and a cleaner dependency ecosystem.
If you are starting a new React project today, Vite is widely considered one of the best options available for building modern web applications.
Top comments (0)