Migrating from Create React App
What you'll learn: How to convert an existing Create React App project to use Vite's faster tooling.
If you have an older React project built with create-react-app (CRA), you can migrate it to Vite to take advantage of faster startup times and Hot Module Replacement. The process involves a few targeted changes to how your project is structured and configured.
The Three Key Changes
1. Move and Update index.html
In CRA, index.html lives in the public/ folder. With Vite, it moves to your project root (next to package.json). You'll also need to add a script tag that loads your JavaScript:
<!-- Add this inside <body> in your root index.html -->
<script type="module" src="/src/main.jsx"></script>
2. Rename Your Entry File
CRA uses src/index.js as the entry point. Rename it to src/main.jsx to match Vite conventions (though any name works if you update the script tag).
3. Replace Build Scripts
In your package.json, swap out the CRA scripts:
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
4. Update Dependencies
Remove react-scripts and install Vite:
npm uninstall react-scripts
npm install -D vite @vitejs/plugin-react
Create a vite.config.js in your project root with the React plugin configured (as you learned in lesson 42).
Key Takeaway: Migrating from CRA to Vite primarily involves moving index.html to the root with a module script tag, renaming your entry file, replacing package.json scripts, and swapping dependencies—most of your component code stays untouched.