LearnContact
Lesson 720 min read

Creating Your First React App

Scaffold a new React project with Vite, install dependencies, start the dev server, and see Hot Module Replacement in action.

Introduction

With Node.js, npm, and your editor ready, it is time to create an actual React project. Rather than assembling a build configuration by hand, you will use Vite's official scaffolding tool, which generates a fully working project in seconds.

This lesson walks through the entire process end to end: creating the project, installing its dependencies, starting the development server, viewing the app in your browser, and finally making a small change to watch React's Hot Module Replacement update the page instantly, without a full reload.

What You Will Learn
  • How to scaffold a new React project using npm create vite@latest.
  • How to install a project's dependencies with npm install.
  • How to start and use the local development server with npm run dev.
  • How to view your running app in the browser.
  • What Hot Module Replacement (HMR) is and how to see it work.

Creating a New Project with Vite

Open your terminal, navigate to the folder where you keep your projects, and run Vite's scaffolding command. The extra "--" separates arguments meant for npm from arguments meant for the Vite scaffolder itself.

Terminal
npm create vite@latest my-app -- --template react
Terminal Output
Scaffolding project in ./my-app...

Done. Now run:

  cd my-app
  npm install
  npm run dev

Here, "my-app" is the folder name for your new project — feel free to name it anything. The "--template react" flag tells Vite to scaffold a plain JavaScript + JSX React project rather than one of its other templates (like Vue or Svelte).

Installing Dependencies

The scaffolded project lists its required packages — React, ReactDOM, and Vite itself — in package.json, but they are not downloaded yet. Move into the new folder and run npm install to download them into a local node_modules folder.

Terminal
cd my-app
npm install
Terminal Output
added 150 packages in 8s
Tip

You only need to run npm install once per project (and again any time you pull new changes that add dependencies). It does not need to be repeated every time you open the project.

Running the Development Server

With dependencies installed, start Vite's local development server. It compiles your code, serves it over HTTP, and watches your files for changes.

Terminal
npm run dev
Terminal Output
VITE v5.2.0  ready in 320 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help

The terminal now shows a Local URL — this is your app, running on your own machine. Leave this terminal window open; the server keeps running (and watching for changes) until you stop it with Ctrl+C.

Opening the App in Your Browser

Open your browser and visit the Local address shown in the terminal, typically http://localhost:5173/. You should see the default Vite + React starter page: the Vite and React logos, a counter button, and some starter text.

Vite + React Logos

Confirms the build tool and library are both wired up correctly.

A Counter Button

A small interactive demo showing React state updating on click.

Edit Instructions

Starter text pointing you to src/App.jsx, the file you will edit next.

Making an Edit & Hot Module Replacement

Open src/App.jsx in your editor and change some visible text — for example, the heading. Save the file, then look back at the browser without refreshing it.

src/App.jsx (before)
function App() {
  return (
    <div>
      <h1>Vite + React</h1>
    </div>
  );
}

export default App;
src/App.jsx (after)
function App() {
  return (
    <div>
      <h1>Hello, my first React app!</h1>
    </div>
  );
}

export default App;
In the Browser (No Manual Refresh)
Hello, my first React app!

The heading updates almost instantly, and if the counter had a value, it would still be there — the page was not reloaded. This is Hot Module Replacement (HMR): Vite sends only the changed module to the browser and React swaps it in, preserving the app's current state.

Why HMR Matters

Without HMR, every code change would force a full page reload, resetting any state you were testing (like text typed into a form). HMR makes the edit-save-check loop dramatically faster, which is a big part of why Vite feels so responsive.

Common Mistakes

Avoid These Mistakes
  • Running npm run dev before running npm install, which fails because dependencies are not downloaded yet.
  • Forgetting to cd into the project folder first, so commands run in the wrong directory.
  • Closing the terminal running the dev server and being confused when the page stops updating.
  • Manually refreshing the browser after every edit out of habit — HMR usually makes this unnecessary.

Best Practices

  • Keep the terminal running npm run dev visible while you work so you can see errors immediately.
  • Save your file after every small change and get in the habit of watching the browser update live.
  • Use Ctrl+C in the terminal to properly stop the dev server when you are done for the day.
  • Commit your project to version control (git) right after scaffolding it, before making changes.
  • Read any red error overlay that appears in the browser carefully — Vite's error messages usually point straight to the problem.

Frequently Asked Questions

Why does the URL use port 5173?

5173 is Vite's default development port. If it is already in use, Vite automatically picks the next available port and shows it in the terminal.

Do I need to run npm install every time I open my project?

No. You only need it once after creating the project, and again if you pull changes that add new dependencies.

What happens if I close the terminal running npm run dev?

The development server stops, and the page in your browser will no longer update or respond. Just run npm run dev again to restart it.

Is the dev server what my users will see in production?

No. npm run dev is only for local development. For production you would run npm run build, which creates optimized static files.

Can I change the port Vite uses?

Yes, either by passing --port when running npm run dev, or by setting the server.port option in vite.config.js.

Key Takeaways

  • npm create vite@latest scaffolds a complete, working React project in seconds.
  • npm install downloads a project's dependencies into node_modules.
  • npm run dev starts a local development server, usually at http://localhost:5173/.
  • Editing and saving a file updates the running app through Hot Module Replacement, without a full page reload.
  • HMR preserves your app's current state while updating the code, making development much faster.

Summary

You now have a real, running React application on your machine, created with three commands and viewable in your browser. More importantly, you have seen the core development loop you will use for the rest of this course: edit a file, save it, and watch the browser update instantly.

In this lesson, you scaffolded a project with npm create vite@latest, installed its dependencies, started the dev server, and watched Hot Module Replacement in action. Next, you will take a closer look at exactly what Vite generated, file by file.

Lesson 7 Completed
  • You created a new React project using Vite.
  • You installed dependencies and started the development server.
  • You viewed your running app in the browser.
  • You watched Hot Module Replacement update your app instantly after an edit.
Next Lesson →

Understanding Project Structure