From Create React App to Vite: A Migration Guide
Aug 02, 2025 am 06:39 AMInstall Vite and plug-ins and create configuration files; 2. Update index.html to point to the modular portal; 3. Adjust the entry file to use modern ES module syntax; 4. Replace the script in package.json to Vite commands; 5. Static resource processing method remains unchanged; 6. Change the environment variable prefix from REACT_APP_ to VITE_ and access it through import.meta.env; 7. Clean up useless dependencies and configuration. The migration process is simple and fast, without rewriting code, which can significantly improve the development experience and build performance. It is recommended to upgrade all CRA users.
Migrating from Create React App (CRA) to Vite is becoming a popular move for React developers looking for faster builds, quicker hot module replacement (HMR), and a more modern development experience. While CRA served the community well, Vite's speed and flexibility make it an attractive alternative. The good news: the migration is straightforward and doesn't require rewriting your entire app.

Here's a practical guide to help you switch smoothly.
? Why Migrate to Vite?
Before jumping into steps, it helps to know why you're doing this:

- Faster startup and HMR : Vite uses native ES modules and serves code on-demand, so your dev server starts in under a second.
- Better build performance : Uses Rollup under the hood, often resulting in smaller, optimized bundles.
- Modern tooling by default : Built-in support for TypeScript, JSX, CSS modules, and more—without config bloat.
- Active development : Vite is actively maintained and embedded by the broader ecosystem (React, Vue, Svelte, etc.).
If your CRA app feels sluggish during development or build times are growing, Vite is a solid upgrade.
? Step-by-Step Migration Guide
1. Install Vite and Required Plugins
Start by adding Vite and the React plugin:

npm install --save-dev vite @vitejs/plugin-react
If you're using TypeScript, you likely already have it — Vite supports it out of the box.
Create a vite.config.js
file in your project root:
// vite.config.js import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [react()], server: { port: 3000, open: true, }, });
This config sets up React support and mirrors CRA's default dev server behavior.
2. Update Your index.html
Move your public/index.html
to src/
(optional but cleaner), or keep it in public
. Then make sure it has a root element and a script tag pointing to your main React file:
<!-- public/index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Your App</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html>
Note: The type="module"
and src="/src/main.jsx"
are key. Vite serves files from the root, so this works.
3. Adjust Your Entry File ( main.jsx
or index.js
)
Rename or update your entry file (usually src/index.js
) to use modern imports and ensure it's compatible with native ES modules.
// src/main.jsx 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> );
Make sure you're using react-dom/client
for React 18.
Also, update file extensions if needed — Vite handles .jsx
, .tsx
, .js
, .ts
seamlessly.
4. Update package.json
Scripts
Replace CRA scripts with Vite equivalents:
"scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview" }
Now you can run:
npm run dev
And your app should load on http://localhost:3000
.
5. Handle Static Assets and Public Files
- Anything in
public/
is served at the root (eg,/robots.txt
,/favicon.ico
) — same as CRA. - Use absolute paths in your code:
src="/logo.png"
works ifpublic/logo.png
exists. - For dynamic imports or assets inside
src/
, useimport asset from './assets/file.png'
— Vite processes these.
No major changes needed here.
6. Environment Variables
Vite uses a different convention:
- Prefix environment variables with
VITE_
to expose them. - Access via
import.meta.env.VITE_SOMETHING
.
Example:
// .env VITE_API_URL=https://api.example.com
// In your code const apiUrl = import.meta.env.VITE_API_URL;
Update any process.env.REACT_APP_*
references to use import.meta.env.VITE_*
.
7. Optional: Clean Up and Optimize
- Remove
react-scripts
:npm uninstall react-scripts
- Delete
src/serviceWorker.js
,src/setupTests.js
if unused. - Consider removing
node_modules
and reinstalling withnpm install
to avoid conflicts.
You can also add @vitejs/plugin-react-refresh
if you want fine-grained HMR control, but @vitejs/plugin-react
(which uses Babel) is usually sufficient.
? Test the Migration
- Run
npm run dev
— does the app load? Are components updating on save? - Run
npm run build
— does it generate files indist/
? - Run
npm run preview
— test the production build locally. - Check environment variables, API calls, and routing (especially if using
BrowserRouter
).
Fix any import path issues or missing extensions (eg, use .js
in imports if unclear).
?? Common Gotchas
Missing file extensions in imports : Vite enforces explicit extensions for
.js
files in some cases. You may need to add.js
or.jsx
in import statements.Babel config ignored : If you relied on custom Babel plugins, you'll need to configure
@vitejs/plugin-react
to use Babel, or switch to SWC viavite-plugin-react-swc
.Proxy setup : If you used
proxy
inpackage.json
, move it tovite.config.js
:server: { proxy: { '/api': 'http://localhost:5000', }, }
? Final Thoughts
Migrating from CRA to Vite is low-risk and high-reward. Most apps transition in under an hour. You keep your code, get faster tooling, and future-proof your setup.
Bottom line : If you're still on CRA, now's a great time to make the switch. Vite delivers a noticeably better developer experience — and your future self will thank you during long coding sessions.
Basically, just install Vite, tweak config and scripts, fix env vars, and go.
The above is the detailed content of From Create React App to Vite: A Migration Guide. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React's main functions include componentized thinking, state management and virtual DOM. 1) The idea of ??componentization allows splitting the UI into reusable parts to improve code readability and maintainability. 2) State management manages dynamic data through state and props, and changes trigger UI updates. 3) Virtual DOM optimization performance, update the UI through the calculation of the minimum operation of DOM replica in memory.

React is a JavaScript library developed by Facebook for building user interfaces. 1. It adopts componentized and virtual DOM technology to improve the efficiency and performance of UI development. 2. The core concepts of React include componentization, state management (such as useState and useEffect) and the working principle of virtual DOM. 3. In practical applications, React supports from basic component rendering to advanced asynchronous data processing. 4. Common errors such as forgetting to add key attributes or incorrect status updates can be debugged through ReactDevTools and logs. 5. Performance optimization and best practices include using React.memo, code segmentation and keeping code readable and maintaining dependability
