Migrating CRA to Vite: A Step-by-Step Guide

TechStaunch Team
March 04, 25 onVite & CRA3 min
Migrating CRA to Vite: A Step-by-Step Guide

How to Future-Proof Your React Projects with Vite’s Speed and Flexibility

Why Migrate to Vite

Vite offers:

  • 70% faster builds than CRA (esbuild vs. Webpack).
  • Instant HMR updates without page reloads.
  • Zero-config simplicity for small-to-medium apps.
  • Native support for modern JS features (Top-Level Await, ES modules).

Prerequisites

  1. Node.js (v18+ recommended).
  2. Existing CRA project with React/TypeScript.
  3. Backup your project (Git branch or cloud storage).

Step 1: Create a New Vite Project

  1. Generate a Vite Template:



    This creates a minimal Vite setup with React.

  2. Copy Source Code:

    • Replace src/ in the Vite project with your CRA src/ folder.
    • Move public/ or assets/ to the Vite root.

Step 2: Update Dependencies

  1. Remove CRA-Specific Packages:

  2. Install Vite Plugins:


    (Use @vitejs/plugin-react for JSX) .

  3. Copy Dependencies:

    • Merge dependencies and devDependencies from your CRA package.json into the Vite project.
    • Remove CRA-specific entries (e.g., eslint-config-react-app).

Step 3: Configure Vite

  1. Create vite.config.ts:


    (Adds @ alias for src/) .

  2. Update Scripts:

Step 4: Fix TypeScript/JSX Configurations

  1. Update tsconfig.json:

  2. Add vite-env.d.ts:

Step 5: Migrate Testing (Optional)

  1. Switch from Jest to Vitest:

  2. Update vite.config.ts:

  3. Add Test Scripts:

Step 6: Handle Environment Variables

  • Rename .env files to .env.vite (Vite’s format).
  • Access variables via import.meta.env.VITE_API_KEY .

Step 7: Configure Proxy Server (If Needed)

  • Add proxy to vite.config.ts:

Step 8: Enable Gzip Compression (Optional)

  1. Install vite-plugin-compression:

  2. Update vite.config.ts:

Step 9: Final Cleanup

  1. Delete Unused Files:
    • Remove node_modules/.cache, build/, and public/*.html (CRA artifacts).
  2. Test Incrementally:
    • Verify HMR, routing, and API calls.

Migrating to Vite can significantly boost your app’s performance and developer experience. If your team needs help with this migration or any development, reach out to us! We specialize in modernizing legacy apps and building scalable React solutions.

Scroll to Top