🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Astro Fundamentals••By Komal Nakrani

Create React App to Astro: A Migration Guide

Migrate from Create React App to Astro: run the app as one island first, then split routes into pages, fix env vars, images and SVGs, and swap Jest for Vitest.

#Astro#React#Create React App#Migration#Vite

To migrate from Create React App to Astro, add the React integration to a new Astro project, copy your src/ components across, and mount the existing app on a single Astro page with client:only="react" so it runs unchanged. Then move routes one at a time into real Astro pages, rendering static parts as HTML and keeping React only for interactive components. Along the way, rename REACT_APP_ variables, update image and SVG imports, and replace react-scripts with Astro’s Vite-based tooling.

Create React App was deprecated by the React team in February 2025, so many teams now need a new home for CRA projects. Astro is a good fit when the app is really a website (marketing pages, docs, a content-heavy product) and you want pages that load as HTML instead of waiting for a JavaScript bundle.

Key Takeaways

  • Phase 1: run the whole CRA app inside one Astro page with client:only="react". It works the same, now on Vite.
  • Phase 2: turn routes into src/pages/ files and drop React Router for those pages.
  • process.env.REACT_APP_X becomes import.meta.env.PUBLIC_X.
  • Image imports return an object in Astro, so use logo.src in React components.
  • Tests move from Jest (react-scripts test) to Vitest with Testing Library.

Is Astro the right destination?

Astro suits CRA projects that are mostly content or marketing with interactive pieces. If the project is a dashboard or app behind a login with heavy client state, a Vite React SPA is a more direct replacement. The Astro vs Vite React SPA comparison covers that decision. Many teams do both: an Astro site for public pages and a Vite SPA for the app.

Step 1: Create the Astro project with React

npm create astro@latest my-site
cd my-site
npx astro add react

astro add react installs @astrojs/react, react and react-dom, and adds the integration to astro.config.mjs. Add any other dependencies from your CRA package.json (React Router, state libraries, UI kits).

Copy CRA’s src/ contents into src/components/app/ (or similar), and copy public/ assets into Astro’s public/. Leave public/index.html behind; Astro layouts replace it.

Step 2: Run the whole app as one island

This gets you running on Astro in minutes without rewriting anything:

---
// src/pages/[...path].astro
import App from '../components/app/App.jsx';
import '../components/app/index.css';

export function getStaticPaths() {
  return [{ params: { path: undefined } }];
}
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My App</title>
  </head>
  <body>
    <App client:only="react" />
  </body>
</html>

client:only="react" skips server rendering and runs the component in the browser, the same way CRA did. If App uses BrowserRouter, configure your host to serve index.html for unknown paths (an SPA fallback) so deep links work, just as you did with CRA.

At this point the site behaves like before, but builds with Vite through Astro, so dev server start-up and rebuilds are much faster.

Step 3: Fix CRA-specific code

A few CRA conventions need changing.

Environment variables. CRA exposed REACT_APP_* variables through process.env. Astro uses Vite’s import.meta.env, and only variables prefixed PUBLIC_ reach browser code:

// before (CRA)
const api = process.env.REACT_APP_API_URL;

// after (Astro)
const api = import.meta.env.PUBLIC_API_URL;

Rename the variables in .env too. For typed, validated variables, Astro’s env.schema config option goes further; see the environment variables guide.

%PUBLIC_URL%. References in HTML or code to %PUBLIC_URL%/logo.png become /logo.png.

Image imports. In CRA, import logo from './logo.png' gave a URL string. In Astro, importing an image gives an object with src, width and height:

// src/components/app/Header.jsx
import logo from './logo.png';

export default function Header() {
  return <img src={logo.src} width={logo.width} height={logo.height} alt="Acme" />;
}

SVG as components. CRA’s import { ReactComponent as Logo } from './logo.svg' doesn’t exist in Astro. Inside React components, use the SVG as an image (<img src={logo.src} />) or add a plugin like vite-plugin-svgr. Inside .astro files, importing an SVG gives you a component you can render directly.

Absolute imports. If CRA’s jsconfig.json set baseUrl: "src", recreate it with path aliases in tsconfig.json ("paths": { "@/*": ["src/*"] }).

Step 4: Move routes into Astro pages

Now take routes out of React Router one at a time. A marketing page that was a React component:

// src/components/app/pages/Pricing.jsx (CRA)
import PlanToggle from '../PlanToggle';

export default function Pricing() {
  return (
    <main>
      <h1>Pricing</h1>
      <p>Simple plans for every team.</p>
      <PlanToggle />
    </main>
  );
}

becomes an Astro page where only the toggle is interactive:

---
// src/pages/pricing.astro
import BaseLayout from '../layouts/BaseLayout.astro';
import PlanToggle from '../components/app/PlanToggle.jsx';
---
<BaseLayout title="Pricing" description="Simple plans for every team.">
  <main>
    <h1>Pricing</h1>
    <p>Simple plans for every team.</p>
    <PlanToggle client:visible />
  </main>
</BaseLayout>

The heading and copy are now HTML in the response, visible to search engines and AI crawlers without running JavaScript. The toggle hydrates when it scrolls into view. Remove the route from the React Router config and replace <Link to="/pricing"> with <a href="/pricing/"> wherever it appears.

Repeat for each route. Static pages (about, legal, blog) usually need no React at all. The client directive guide helps choose between client:load, client:idle and client:visible for what remains. Once every route is a page, delete the catch-all [...path].astro.

Step 5: Shared state between islands

In CRA, a React context at the root wrapped the whole app. Separate islands on an Astro page don’t share a React tree, so context doesn’t span them. Options:

  • Combine tightly connected components into one island.
  • Share state through a small store such as Nano Stores (nanostores with @nanostores/react).
  • Keep state in the URL or localStorage when it needs to survive page navigation.

For an authenticated area that relies on global context everywhere, keep it as a single client:only island on one page.

Step 6: Data fetching

CRA fetched data in useEffect after the page loaded. In Astro pages, fetch at build time in the frontmatter, so the data is in the HTML:

---
// src/pages/changelog.astro
import BaseLayout from '../layouts/BaseLayout.astro';
const res = await fetch('https://api.example.com/releases');
const releases = await res.json();
---
<BaseLayout title="Changelog" description="What's new in each release.">
  <ul>
    {releases.map((r) => <li><strong>{r.version}</strong> {r.summary}</li>)}
  </ul>
</BaseLayout>

Data that’s per-user or must be live stays in client-side React, or moves to on-demand routes with an adapter. See which rendering mode to use.

Step 7: Tests, build scripts and deploy

Replace react-scripts scripts in package.json:

{
  "scripts": {
    "dev": "astro dev",
    "build": "astro build",
    "preview": "astro preview",
    "test": "vitest"
  }
}

Jest tests written with Testing Library port to Vitest with few changes: install vitest, jsdom and @testing-library/react, set the test environment to jsdom, and replace jest.fn() with vi.fn(). Astro provides getViteConfig() from astro/config so Vitest uses the same configuration as your site.

Build output goes to dist/ instead of build/. Update your host’s publish directory, then deploy; the static deployment docs list the settings for each host. The official CRA migration guide in the Astro docs covers the same process.

If the public side of your project is a product or SaaS site, Astro themes in the Technology category give you pricing, features and docs pages to drop your React islands into.

FAQ

Is Create React App deprecated?

Yes. The React team deprecated Create React App in February 2025 and recommends moving to a framework or a build tool like Vite. Existing CRA projects still run, but they won’t get new features.

Can Astro run my existing React app?

Yes. Install the React integration and render your root App component on an Astro page with client:only="react". It runs in the browser as before, and you can move routes into Astro pages gradually.

How do environment variables change from CRA to Astro?

Rename REACT_APP_ variables to use the PUBLIC_ prefix and read them with import.meta.env.PUBLIC_NAME instead of process.env.REACT_APP_NAME. Variables without PUBLIC_ are only available on the server and at build time.

Should I migrate CRA to Astro or Vite?

Choose Vite with React if the project is an app behind a login with lots of client state. Choose Astro if it’s mainly public pages that should load fast and be found in search, with interactive React components in places.

Do I have to rewrite my React components for Astro?

No. React components work in Astro as they are. You rewrite pages and layouts as .astro files over time, and add client:* directives to the components that need to be interactive.