Rutik Bhosale

Mar 11, 2025 • 4 min read

Migrating from Create React App (CRA) to Vite: Common Issues and Solutions

Migrating from Create React App (CRA) to Vite: Common Issues and Solutions

Why Migrate from CRA to Vite?

Create React App (CRA) has been a popular tool for setting up React projects, but as modern development practices evolve, developers are shifting towards Vite for better performance, faster build times, and an improved development experience.

Benefits of Vite over CRA:

  • Faster Development Server: Vite uses native ES modules and on-demand compilation, making the development server significantly faster than CRA.

  • Optimized Build Process: Vite leverages Rollup for optimized builds, reducing bundle size.

  • Hot Module Replacement (HMR): Vite’s HMR is more efficient and faster than CRA.

  • Better Plugin Ecosystem: Vite offers a lightweight and modern plugin system.

  • No Ejecting Required: Unlike CRA, you don’t need to eject to customize configurations.

Migrating from CRA to Vite can significantly enhance your development workflow, but you may encounter some challenges during the transition. This guide will help you troubleshoot and resolve common issues.

Step-by-Step Migration Process

Step 1: Uninstall React Scripts

npm uninstall react-scripts

CRA uses react-scripts, which needs to be removed before switching to Vite.

Step 2: Install Vite Dependencies

npm install vite @vitejs/plugin-react --save-dev

Ensure you install the correct Vite plugin for React.

Step 3: Move index.html to the Root Directory

CRA keeps index.html inside the public folder, but Vite requires it at the root level.

Step 4: Replace %PUBLIC_URL% in index.html

Replace all instances of %PUBLIC_URL% with / in your index.html file.

Step 5: Update script Tag in index.html

Add the following script inside the <body> tag:

<script type="module" src="/src/index.tsx"></script>

Ensure the src path is correctly set to your main entry file.

Step 6: Create vite.config.js at the Root

Create a new file named vite.config.js in the root directory of your project.

Step 7: Configure vite.config.js

Add the following configuration:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
  plugins: [react()],
});

Step 8: Update Scripts in package.json

Modify your package.json scripts to use Vite:

"scripts": {
  "dev": "vite --mode dev",
  "build": "vite build",
  "serve": "vite preview"
}

Step 9: Handling Base URL Issues in API Calls

If your app does not recognize the base URL when making API requests, update vite.config.js:

export default defineConfig({
  base: '/',
});

Step 10: Replacing process.env with import.meta.env

Vite does not support process.env like CRA does. Instead, replace all instances of process.env with import.meta.env.

Step 11: Update Environment Variables

Vite requires environment variables to start with VITE_. Rename your .env variables accordingly:

VITE_API_URL=https://your-api-url.com

Then access them in your code using:

const apiUrl = import.meta.env.VITE_API_URL;

Common Issues and Solutions

1. Vite Runs on a Different Port than CRA

By default, Vite runs on a different port. To match your CRA port (e.g., 3000), update vite.config.js:

export default defineConfig({
  server: {
    port: 3000,
  },
});

2. Multiple Bundles Appear in Network Tab

In development mode, Vite uses Hot Module Replacement (HMR), which loads multiple JavaScript bundles. This is normal and improves the development experience. To check production behavior, run:

npm run build
npm run preview

This will serve your built project, showing fewer bundles.

3. TypeScript Error: Property 'env' does not exist on type 'ImportMeta'

If you see this error when using import.meta.env, create a vite-env.d.ts file in src:

/// <reference types="vite/client" />
interface ImportMetaEnv {
  readonly VITE_API_URL: string;
  // add your env variable here...
}
interface ImportMeta {
  readonly env: ImportMetaEnv;
}

This helps TypeScript recognize Vite’s environment variables.

4. Build Folder Not Found in CI/CD

Vite outputs a dist folder instead of build. Update your CI/CD configuration to use dist instead.

5. Node.js Version Requirement

Ensure you’re using Node.js version 16 or later for Vite compatibility.

6. Ignoring dist Folder in Git

If your dist folder appears in Git changes, add it to .gitignore:

/dist

7. Dependency Issues After Migration

If you encounter dependency issues, try:

rm -rf node_modules package-lock.json
npm cache clean --force
npm install

This will clean and reinstall dependencies.

8. Optimizing Large Bundles in Production

If any chunk in your production build exceeds 1MB, manually split them using:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            if (id.includes("lodash")) return "vendor-lodash";
            if (id.includes("react-quill")) return "vendor-quill";
            // You can add more dependencies here to create separate chunks/
            return "vendor";
          }
        },
      },
    },
  },
});

This reduces large bundle sizes.

9. Organizing Media Files in the Build Output

To structure your build files properly, update vite.config.js:

build: {
  rollupOptions: {
    output: {
      assetFileNames: (assetInfo) => {
        const name = assetInfo.name || "unknown";
        if (name.endsWith(".css")) return "static/css/[name]-[hash][extname]";
        if (/\.(png|jpg|jpeg|gif|svg)$/.test(name)) {
          return "static/media/[name]-[hash][extname]";
        }
        return "static/assets/[name]-[hash][extname]";
      },
    },
  },
}

This will separate JavaScript, CSS, and media files in the dist folder.

Migrating from CRA to Vite improves performance but requires some adjustments. By following these steps, you can ensure a smooth transition and fix common issues. If you encounter any additional problems, refer to the Vite documentation for further guidance.

Happy coding! 😊

Join Rutik on Peerlist!

Join amazing folks like Rutik and thousands of other people in tech.

Create Profile

Join with Rutik’s personal invite link.

1

8

1