How to deploy a React app to hosting with Vite or CRA

Deploying a React app to hosting

A typical React app does not need a server running React. After npm run build, you have a folder of static HTML, CSS and JavaScript, and the browser does the rest. Understanding this helps you avoid most errors when you deploy React to hosting.

This guide is about React single-page apps using Vite or Create React App. Next.js works differently, so see Next.js hosting for that.

What does the build produce?

npm install
npm run build
ToolOutput folder
Vitedist
Create React Appbuild

Inside are index.html and an assets folder. This is what goes on the host, not the src folder.

Option 1: Upload the finished build

Good when you rarely update the site.

  1. Run npm run build.
  2. Go into dist, select all files and zip them so that index.html is at the top level.
  3. Open a new deployment and choose Upload ZIP.

On cPanel hosting, upload these files to public_html. The downside is that each change means building and uploading again.

Option 2: Deploy from GitHub and build on the server

This is more convenient if you update often. VibeVN recognises Vite and Create React App projects, builds them and serves the output folder, so you do not need to change anything.

On another host, a Vite project may need preparing. Vite has no start script, so add one:

{
  "scripts": {
    "build": "vite build",
    "start": "vite preview --host 0.0.0.0 --port 3000"
  }
}

Then allow your real domain in vite.config, or vite preview returns a 403 error:

export default defineConfig({
  plugins: [react()],
  preview: { allowedHosts: true },
});

The details and our test results are in deploying Lovable, Bolt, v0 and Cursor projects, because those tools mostly generate Vite projects.

For Create React App, install serve and use "start": "serve -s build -l 3000". Keep serve in dependencies so it is present at run time.

The 404 error when refreshing a page

This is the classic React error: the home page works, clicking through to /products works, but pressing F5 there gives a 404.

The reason is that React Router runs in the browser. Clicking a link sends nothing to the server. Refreshing asks the server for a file called /products, which does not exist. The fix is to have the server return index.html for any path that matches no file:

  • vite preview: handles this already. We tested it and sub-paths return the right page.
  • serve: use the -s flag as above.
  • Your own Nginx: add try_files $uri /index.html;.
  • Apache or cPanel: use .htaccess with a rewrite rule to index.html.

A blank page after deploying

Open the browser console (F12). If .js files return 404, check the base path:

  • Vite: base in vite.config, usually /.
  • CRA: homepage in package.json. Remove any leftover value from GitHub Pages.

If the site lives in a sub-folder such as example.com/app, both must match that path.

Environment variables in React

In a single-page app, every variable is bundled into the JavaScript sent to the browser:

  • Vite uses the VITE_ prefix, and CRA uses REACT_APP_.
  • Anyone can read these values. Never put a secret such as an OpenAI key here. If you need to call an API with a secret key, go through a backend.
  • Values are fixed at build time, so changing one means building again.

Is a single-page app good for SEO?

A React SPA sends almost empty HTML, and the content appears after JavaScript runs. Google can process JavaScript, but not always quickly or completely. If SEO matters, consider Next.js or pre-rendering pages before deploying. For a dashboard or anything behind a login, an SPA is perfectly fine.

After deploying

Try the site on a phone, refresh some inner pages, then point your own domain at it. See how to host a website from GitHub for other options.

Frequently asked questions

Does React need Node.js hosting?

Not necessarily, because the build output is static files. Node.js is only needed for the build, or if you use vite preview or serve to serve the files.

Can I deploy React on shared hosting?

Yes. Build on your machine, upload the output to public_html, and configure .htaccess to handle inner paths.