SovranCode
Learn
Learn on SovranCodeCourses7 free learning paths→ExercisesPractice with live challenges→GuidesDirect answers for developers→E-booksFocused field guides→
Build
Build on SovranCodeTemplatesSovranCode team marketplace→Developer toolsFast browser utilities→
Connect
Connect on SovranCodeForumQuestions and discussions→JournalPractical development notes→AboutWhy SovranCode exists→PricingFree, Plus, and Student Plus→
Services
SearchCreate account
Explore SovranCodeLearn, practice, and build.
LearnCourses7 free learning paths→ExercisesPractice with live challenges→GuidesDirect answers for developers→E-booksFocused field guides→
BuildTemplatesSovranCode team marketplace→Developer toolsFast browser utilities→
ConnectForumQuestions and discussions→JournalPractical development notes→AboutWhy SovranCode exists→PricingFree, Plus, and Student Plus→
Search
Guides/Web Development/How to Create a React App with Vite
All guides
How-to guide

How to Create a React App with Vite

Create a modern React app with Vite, understand the generated project, build a real component, use environment variables safely, and produce a deployment-ready build.

Beginner 21 min readUpdated September 17, 2026
Compose Develop Ship
React component modules flowing through a fast development path into a browser preview
SOURCE fast feedback BUILD
QUICK ANSWER

Install a Node.js version supported by the current Vite release, then run npm create vite@latest my-react-app -- --template react. Enter the project, install dependencies with npm install, and start development with npm run dev. Use the react-ts template instead when you want TypeScript. Before deployment, run npm run build and inspect the generated dist folder with npm run preview.

01

Before you create the app

Vite is a frontend build tool with a fast development server and an optimized production build. The React template configures JSX, React Fast Refresh, linting, and the scripts needed for a client-side application. It deliberately does not choose routing, data fetching, authentication, testing, or a backend for you.

You need

  • A supported Node.js release and npm. The current Vite documentation requires Node.js 20.19+ or 22.12+; a template may require a newer release.
  • A terminal opened in the parent folder where the project should be created.
  • A code editor such as VS Code or another editor with JavaScript and JSX support.
  • A modern browser for Vite's development server.
  • Git if you plan to track the project and publish it to a remote repository.
Verify the prerequisites
node --version
npm --version
git --version
Node 20 is not specific enough

Vite's current requirement is Node.js 20.19+ or 22.12+, not every Node 20 or Node 22 release. If create-vite reports an unsupported engine, update Node.js rather than bypassing the warning.

Missing a prerequisite?

Install Node.js and npmUse the SovranCode setup and PATH verification guide.Install Git and configure GitHubPrepare version control and remote authentication.
02

Decide whether Vite is the right React starting point

Vite is a strong choice for learning React, browser-only dashboards, embedded interfaces, prototypes, and client-rendered single-page applications. It gives you a clean build foundation without hiding the application structure.

Choose the starting point by application requirements
ProjectStart withWhy
Learning React or a client-only SPAVite + ReactSmall setup, fast feedback, and direct control over routing and data choices.
Marketing site that needs strong per-page SEOA React framework or static-site frameworkServer rendering or pre-rendering is usually part of the architecture, not an afterthought.
Full-stack React applicationA recommended full-stack React frameworkRouting, server code, data loading, forms, and deployment conventions are integrated.
React inside an existing server-rendered siteVite integration or incremental ReactAdd isolated interactive roots without rebuilding the entire application.
Create React App is deprecated

The React documentation no longer recommends Create React App for new projects. Vite is a suitable build-tool option when a full-stack framework is not the right fit.

03

Create the Vite and React project

The create-vite initializer copies a maintained template into a new folder. With npm 7 and newer, the extra double dash passes the template option through npm to create-vite.

Create a React project with JavaScript
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
Use the standard react template first

Choose react unless you intentionally want the React Compiler template. You can adopt additional compiler behavior after you understand the baseline project.

Equivalent scaffold commands
Package managerReact JavaScript template
npmnpm create vite@latest my-react-app -- --template react
pnpmpnpm create vite my-react-app --template react
Yarnyarn create vite my-react-app --template react
Bunbun create vite my-react-app --template react
Do not mix lockfiles

Choose one package manager per repository. Commit its lockfile and remove lockfiles created by other package managers rather than alternating between npm, pnpm, Yarn, and Bun.

An abstract Vite workflow moving from a project scaffold through components and live preview to a production bundle
Scaffold the project, compose the interface, develop with fast feedback, and verify the optimized build.
04

Open and verify the development app

  1. 01

    Read the terminal URL

    npm run dev starts Vite's development server and prints its local address. Open the exact URL shown rather than assuming the port is always the same.

  2. 02

    Make one visible edit

    Open src/App.jsx or src/App.tsx, change the heading, and save. The browser should update through React Fast Refresh without a full reload.

  3. 03

    Check the browser console

    Open developer tools and resolve red errors or failed network requests. A page that looks correct can still have runtime problems.

  4. 04

    Stop the server deliberately

    Return to the terminal and press Ctrl+C. The development server is a running process; closing a browser tab does not stop it.

Useful development-server options
# Start normally
npm run dev

# Expose to other devices on your local network
npm run dev -- --host

# Request a specific port and fail if it is occupied
npm run dev -- --port 5173 --strictPort
Treat --host as network exposure

The --host option can make the development server reachable from other devices on the network. Use it only on a network you trust, and never treat the Vite development server as a production server.

05

Understand the generated project structure

Important files in a standard React template
PathPurposeFirst-day guidance
index.htmlThe HTML entry document Vite transforms and serves.Keep the root element that React mounts into; update metadata as the app becomes real.
src/main.jsxCreates the React root and renders the top-level component.Providers, global CSS, and app-wide setup often begin here.
src/App.jsxThe starter application component.Replace the demo, then split features into focused components.
src/assets/Source assets imported by application code.Imported files are processed, hashed, and included in the build graph.
public/Files copied to the build root without transformation.Reference them from root paths and use this folder only when transformation is unwanted.
vite.config.jsVite plugins and build or server configuration.Keep changes minimal; defaults are enough for the first app.
eslint.config.jsLint rules for the generated source.Run the lint script before committing.
package.jsonDependencies and project scripts.Commit it together with package-lock.json.
A practical component structure after the starter
src/
  assets/
  components/
    ProjectCard.jsx
  data/
    projects.js
  App.jsx
  App.css
  index.css
  main.jsx
Organize by feature when the app grows

A small components folder is fine at first. When features gain their own components, hooks, API code, and tests, group those files by feature rather than building one enormous global folder for each file type.

06

Replace the starter with a useful React component

The example below proves the essential React loop: component state changes in response to an event, React renders the new value, and Vite updates the module quickly while you work.

src/App.jsx
import { useState } from 'react'
import './App.css'

export default function App() {
  const [tasks, setTasks] = useState(0)

  return (
    <main className="app-shell">
      <p className="eyebrow">VITE + REACT</p>
      <h1>Your project is ready.</h1>
      <p>You have completed {tasks} setup {tasks === 1 ? 'task' : 'tasks'}.</p>
      <button type="button" onClick={() => setTasks((count) => count + 1)}>
        Complete a task
      </button>
    </main>
  )
}
src/App.css
.app-shell {
  width: min(680px, calc(100% - 2rem));
  margin: 10vh auto;
  padding: clamp(2rem, 6vw, 4rem);
  border: 1px solid #c9c7df;
  border-radius: 1.25rem;
  background: #ffffff;
  box-shadow: 0 24px 70px rgba(37, 32, 73, 0.12);
}

.eyebrow {
  color: #6656d9;
  font-weight: 800;
  letter-spacing: 0.12em;
}

button {
  padding: 0.8rem 1rem;
  border: 0;
  border-radius: 0.65rem;
  color: white;
  background: #5b48d6;
  cursor: pointer;
}
Component names start with a capital letter

React treats lowercase JSX names as built-in HTML elements and capitalized names as your components. Name files and exported components consistently so imports remain easy to follow.

07

Know what the Vite scripts actually do

Scripts generated by the React template
CommandPurposeImportant limit
npm run devStarts the development server with module updates and React Fast Refresh.Development only; it is not the production server.
npm run buildCreates optimized static production assets in dist.A successful build does not prove routing or runtime API behavior on the host.
npm run lintRuns the generated ESLint configuration against source files.Lint catches defined rule violations, not every application bug.
npm run previewServes the already-built dist directory for a local production check.It is a preview tool, not a production server.

Hot Module Replacement updates changed modules while preserving as much application state as possible. React Fast Refresh builds on that behavior for components. When a change cannot be refreshed safely, a full reload is expected.

Run the build before the end of the day

Development mode can hide production-only failures. Build and preview regularly, especially after changing imports, environment variables, asset paths, or routing.

08

Install and remove project dependencies

Install libraries inside the project folder so they are declared in package.json. Application code should be able to recreate its dependency tree from the committed manifest and lockfile.

Common npm dependency commands
# Runtime dependency used by application code
npm install date-fns

# Development-only tool
npm install --save-dev vitest

# Remove a package cleanly
npm uninstall date-fns

# Inspect direct dependencies
npm list --depth=0

Before adding a package

  • Confirm the package solves a real requirement that is not simpler to implement directly.
  • Check its documentation, maintenance activity, license, and supported React version.
  • Review the package name carefully to avoid a lookalike dependency.
  • Understand whether it adds browser bundle weight or requires server-only APIs.
  • Commit package.json and package-lock.json together after the change.
09

Use environment variables without leaking secrets

Vite exposes built-in mode values through import.meta.env. Custom variables must use the VITE_ prefix to become available in client code. That prefix means public-to-the-browser, not secure.

.env.local
VITE_API_BASE_URL=https://api.example.test
# Never put a private database password or server secret here.
Read the value in application code
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL

if (import.meta.env.DEV) {
  console.info('Using API:', apiBaseUrl)
}
Vite environment files and precedence
FileLoaded whenCommit?
.envEvery mode.Only when it contains non-secret shared defaults.
.env.localEvery mode, with higher priority than .env.No; keep *.local ignored.
.env.developmentDevelopment mode.Only for safe shared development values.
.env.productionProduction mode during the normal build.Only for safe shared production values.
.env.[mode].localOne mode with local priority.No.
VITE_ values are public

VITE_ variables are statically included in browser code at build time. Never place database passwords, private API keys, signing secrets, or privileged tokens in them. Protect secrets behind a backend or serverless function.

Restart after editing .env files

Vite loads environment files when the process starts. Stop and restart npm run dev after adding or changing an environment variable.

10

Handle images, public files, and base paths correctly

Import an image from src/assets
import dashboardImage from './assets/dashboard.png'

export function Hero() {
  return <img src={dashboardImage} alt="Project dashboard" />
}

Imported assets participate in the module graph. Vite can process them, give production files cache-friendly names, and report a missing file during the build.

Subpath deployment needs a base

When deploying under a path such as /my-repository/ instead of the domain root, configure Vite's base option and ensure client-side routing uses the same deployment location.

11

Build, preview, and deploy the application

  1. 01

    Create the production bundle

    Run npm run build. Vite uses index.html as the default entry and writes optimized assets to dist unless the configuration changes the output directory.

  2. 02

    Preview the exact build

    Run npm run preview and open the printed URL. Test navigation, refreshing a nested route, API calls, responsive layouts, and browser-console errors.

  3. 03

    Deploy the dist output

    Configure the hosting platform to run npm ci and npm run build, then publish dist. Do not upload src as the finished site.

  4. 04

    Configure SPA fallback when routing

    A client-side router needs the host to return index.html for application routes. Without a fallback, refreshing /settings can produce a host-level 404 even though in-app navigation works.

Production verification
npm run lint
npm run build
npm run preview
Typical static-host settings
SettingValueWhy
Install commandnpm ciUses the committed package-lock.json exactly.
Build commandnpm run buildCreates the optimized site.
Publish directorydistThis is Vite's default build output.
Environment variablesSet in the host dashboardProduction values are injected when the host builds the app.
SPA rewriteAll app routes to /index.htmlAllows the client router to handle direct visits and refreshes.
vite preview is not a production server

The preview command is a local inspection tool for dist. Use a real static host, CDN, or documented server integration for production.

12

Put the Vite app under version control

The template includes a useful .gitignore. Review it before the first commit, especially after adding local environment files or tool-specific output.

Create the first project commit
git init
git add .
git status
git commit -m "Create React app with Vite"

# After creating an empty GitHub repository
git remote add origin YOUR_REPOSITORY_URL
git push -u origin main

Commit these, not generated local state

  • Commit source files, public assets, configuration, package.json, and package-lock.json.
  • Do not commit node_modules; npm recreates it from the manifest and lockfile.
  • Do not commit .env.local or files containing credentials.
  • Commit intentional configuration changes instead of undocumented editor-only workarounds.
  • Run lint and build before pushing a branch for review.
13

Fix common React and Vite setup problems

Start with the symptom you can reproduce
ProblemLikely causeFix
Unsupported engine or Node versionThe installed Node.js release is below Vite or template requirements.Check node --version and install a supported LTS patch; do not use --force to hide the requirement.
npm create vite failsRegistry access, proxy configuration, or an outdated Node/npm environment.Check npm config get registry, network policy, and the first error line; retry after fixing the cause.
Port already in useAnother development server owns the preferred port.Use the URL Vite selects, stop the other process, or pass --port with --strictPort.
Blank pageA runtime exception, wrong root element, or broken import prevents rendering.Open the browser console and terminal, then fix the first error rather than changing random files.
Module not foundThe package is not installed, the path or letter case is wrong, or the terminal is in another folder.Confirm pwd, inspect package.json, and match the actual filename exactly.
Changes do not appearThe file is not imported, the server is stale, or the browser is showing another port.Check the terminal URL and import path; restart the server when config or env files changed.
Environment variable is undefinedIt lacks the VITE_ prefix or the server has not restarted.Rename only client-safe values with VITE_, restart Vite, and read import.meta.env rather than process.env.
Nested route returns 404 after deploymentThe static host lacks SPA fallback or the base path is wrong.Configure the host rewrite and align Vite base plus router settings with the deployed path.
A useful diagnostic snapshot
node --version
npm --version
npm list vite react react-dom
npm run lint
npm run build
Avoid deleting everything first

Read the first terminal and browser-console errors before removing node_modules. Most failures are a Node requirement, wrong folder, wrong import case, stale environment process, or host routing rule.

14

Choose the next capability deliberately

Common next steps after the base app works
NeedAdd deliberatelyQuestion to answer first
Multiple screensA maintained routerWill this remain a client SPA, or does the app need framework-level data and rendering?
Remote dataA small API client and explicit loading, error, empty, and success statesWhere are credentials kept, and what is the API's browser security model?
Component testsA test runner and DOM testing utilitiesWhich user behavior must remain stable?
FormattingA formatter integrated with the existing lint workflowCan the team run one documented command locally and in CI?
Server rendering or backend routesA React framework or documented backend integrationHas the project outgrown a client-only static build?

Primary learning references

Vite guideCurrent scaffolding commands, requirements, and concepts.React Quick StartComponents, JSX, state, events, and rendering.Vite production deploymentOfficial build, preview, base-path, and hosting guidance.

Frequently asked questions

What command creates a React app with Vite?

With npm, run npm create vite@latest my-react-app -- --template react. Then enter the folder, run npm install, and start the development server with npm run dev.

Should I choose the react or react-ts template?

Choose react for JavaScript or react-ts when you want TypeScript from the beginning. Both use the same Vite scripts and React development workflow.

Why is there an extra double dash in the npm command?

npm uses -- to pass the following options to create-vite. Without it, --template may be interpreted by npm instead of the initializer.

Is Vite a React framework?

No. Vite is a build tool and development server. Its React template configures a client application, but you choose routing, data loading, authentication, backend behavior, testing, and deployment architecture.

Should I still use Create React App?

No for a new project. Create React App is deprecated. Use a recommended React framework for integrated application architecture, or a build tool such as Vite when a client-side app or from-scratch setup is appropriate.

Which Node.js version does Vite require?

The current Vite documentation requires Node.js 20.19+ or 22.12+, and a template can require a newer release. Check the official Vite guide when installing because requirements change across major releases.

What is the difference between npm run dev and npm run preview?

npm run dev serves source modules with development features such as Fast Refresh. npm run preview serves the already-generated dist build for local inspection. Neither command is intended to be the production server.

Where does Vite put the production build?

By default, npm run build writes the optimized static output to dist. Configure your static host to publish that folder unless vite.config changes build.outDir.

Are VITE_ environment variables secret?

No. Values prefixed with VITE_ are exposed to client code and included in the browser bundle. Keep sensitive credentials on a backend or in serverless functions.

Why does refreshing a React route return 404 after deployment?

The host is looking for a physical file at that path. Configure a single-page-application rewrite to index.html and ensure the Vite base and client router match the deployment path.

Can I deploy a Vite React app to static hosting?

Yes. Build the app and deploy the dist directory. Configure production environment values, the correct base path, and an SPA fallback when client-side routes are used.

On this page
Before you create the appDecide whether Vite is the right React starting pointCreate the Vite and React projectOpen and verify the development appUnderstand the generated project structureReplace the starter with a useful React componentKnow what the Vite scripts actually doInstall and remove project dependenciesUse environment variables without leaking secretsHandle images, public files, and base paths correctlyBuild, preview, and deploy the applicationPut the Vite app under version controlFix common React and Vite setup problemsChoose the next capability deliberatelyFAQ
CONTINUE LEARNING

Put the answer to work.

COURSEJavaScript courseCOURSECSS courseE-BOOKJavaScript: From Values to Applications
RELATED GUIDES

Keep moving.

Browse all guides
How-to guideBeginner

How to Install Node.js and npm: Complete Setup Guide

Install Node.js and npm correctly on Windows, macOS, or Linux, verify your setup, create a first project, and fix the most common PATH and permission problems.

20 min readUpdated Sep 2026
Open guide
How-to guideBeginner

How to Install Git and Configure GitHub

Install Git on Windows, macOS, or Linux, configure your identity and default branch, connect securely to GitHub, and push your first repository without guessing.

22 min readUpdated Sep 2026
Open guide
SOURCES

Official documentation

  • Vite — Getting Started
  • Vite — Features
  • Vite — Environment Variables and Modes
  • Vite — Building for Production
  • Vite — Deploying a Static Site
  • React — Installation
  • React — Creating a React App
  • React — Quick Start
THE SOVRANCODE PLATFORM

Learn enough to build something real.

Start learning
SovranCode

A free-first programming platform for people who learn best by understanding, practicing, and building.

● Core learning content is free
FollowYouTubePinterestX
LearnHTML courseCSS courseJSJavaScript coursePython courseC courseSQL courseGit courseDeveloper guidesAll exercises
BuildTemplate marketBuyer libraryTemplate licensesDeveloper toolsE-books
CommunityForumJournalContact
CompanyPricing previewAboutServicesPrivacyEnglish edition. Additional languages will be published only after full editorial review.
© 2026 SovranCode · Built for curious minds.Next.js · Node.js · PostgreSQL