Thuta Learning
React
BasicWeb DevelopmentIntermediate

Get Started (Vite)

ReactLesson 325 min

What you'll walk away with

  • Check node -v and npm -v, then create a React project with npm create vite@latest
  • Explain what npm install, npm run dev, npm run build and npm run preview each do
  • Trace the startup path index.html, then src/main.jsx, then src/App.jsx
  • Know that Create React App is deprecated and why this course uses Vite

Explain it simply

Explain it simply

To write React you need a kitchen. Node.js is the stove, the engine that runs JavaScript tools on your computer. npm is the grocery store that downloads packages. Vite is the cook: it turns your JSX files into JavaScript the browser understands and opens a preview server on your machine.

A handful of commands gives you a project with a clean folder structure, and every time you save a file the browser shows the change almost instantly.

Problem → Why → Solution

The problem: JSX dropped into an HTML file does not run

Many beginners create an index.html, paste the intro lesson's code into a <script>, and double-click the file to open it. The page stays blank and the Console shows "Uncaught SyntaxError: Unexpected token '<'". Removing the JSX only leads to the next error: the browser cannot find a module called "react" to import.

index.html
<!doctype html>
<html>
  <body>
    <div id="root"></div>
    <script type="module">
      import { createRoot } from "react-dom/client"; // browser: cannot resolve
      createRoot(document.getElementById("root")).render(<h1>Hi</h1>); // SyntaxError
    </script>
  </body>
</html>

Why does it fail?

Browsers understand JavaScript, not JSX, so the first < is a syntax error. They also have no idea how a bare package name like "react-dom/client" maps to a file inside node_modules. A build tool such as Vite sits in between: it compiles JSX and resolves package imports for you.

Core concept: what a Vite project is made of

To begin, install Node.js from nodejs.org (the LTS version); npm comes with it. Current Vite releases need a recent Node version, so a very old Node install can produce an engine error or strange crashes; check the Vite documentation for the exact minimum.

npm create vite@latest downloads the latest version of a tool called create-vite and generates a folder from the template you choose: react for JavaScript or react-ts for TypeScript. Newer versions of create-vite may ask extra questions, such as which linter to use or whether to install and start right away.

File or folderIts job
index.htmlThe page shell: contains <div id="root"> and a script tag that loads /src/main.jsx
src/main.jsxThe entry file: renders <App /> inside <StrictMode> with createRoot()
src/App.jsxThe root component and the first file you edit
src/assets, publicStatic files such as images and icons
package.jsonDependencies (react, react-dom, vite) and npm scripts
vite.config.jsVite settings; enables @vitejs/plugin-react for JSX

Scripts in package.json run with npm run. npm run dev starts the development server, which uses Hot Module Replacement (HMR) to swap only the file you saved into the browser, so the page does not fully reload. npm run build writes optimised static files to the dist/ folder, and npm run preview serves that dist/ folder locally so you can check it before deploying.

The React team deprecated Create React App (create-react-app) in 2025. react.dev recommends a framework such as Next.js or React Router for full apps, and a build tool such as Vite when you want to start from scratch. This course uses Vite because it is the simplest setup and keeps the focus on React itself.

Mental model: how the page starts up
  1. Browser opens index.html
  2. A script loads src/main.jsx
  3. main.jsx renders <App /> into #root
  4. Saving App.jsx triggers an HMR update

Vite sits between the browser and your files, translating JSX on the fly.

General pattern: project lifecycle commands

bash
npm create vite@latest <app-name> -- --template <react | react-ts>
cd <app-name>
npm install       # once, and again after dependencies change
npm run dev       # dev server with hot updates
npm run build     # optimised production files in dist/
npm run preview   # serve dist/ locally to check the build

The first two lines run once per project. The bare -- passes the --template flag through npm to create-vite. The remaining commands are the ones you use day to day inside the project folder.

Example: creating your first React project

Open a terminal (PowerShell or Git Bash on Windows, Terminal on a Mac), move to the folder where you keep projects, and run these commands in order. Lines starting with # are comments; you do not type them. The whole process takes a minute or two, most of it spent downloading packages during npm install.

terminal
# 0. Check that Node.js and npm are installed
node -v
npm -v

# 1. Create a React project with Vite
npm create vite@latest my-react-app -- --template react

# 2. Move into the project folder
cd my-react-app

# 3. Install project dependencies
npm install

# 4. Start the local development server
npm run dev

Code breakdown

  1. Step 0node -v

    Confirms Node.js is installed and shows its version. If you see "command not found" or "not recognized", install Node.js, then close and reopen the terminal.

  2. Step 1npm create vite@latest my-react-app -- --template react

    Creates a new my-react-app folder from the react template. @latest uses the newest create-vite; if it asks extra questions, the defaults are fine.

  3. Step 2cd my-react-app

    Every later command must run where package.json lives, so you move into the project folder.

  4. Step 3npm install

    Downloads react, react-dom, vite and the other packages listed in package.json into node_modules, and records exact versions in package-lock.json.

  5. Step 4npm run dev

    Starts the development server, which keeps running until you press Ctrl+C. If port 5173 is busy, Vite picks the next free one, such as 5174.

You should see
node -v and npm -v print version numbers. After npm run dev the terminal shows something like this (versions and timings vary):

  VITE v8.x.x  ready in 350 ms

  ➜  Local:   http://localhost:5173/

Opening http://localhost:5173/ in a browser shows the starter page with the Vite and React logos and a "Count is 0" button.

Replacing the starter code with your own

You rarely need to edit src/main.jsx: it imports App and renders it inside StrictMode. In development only, StrictMode calls your components twice to expose impure code, so a console.log that prints twice is not a bug. The file you edit is src/App.jsx; you can delete all the starter content and begin with a tiny component.

src/main.jsx (generated)
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>,
)

In real projects

In real projects

After cloning a team project with git, the first thing you run is npm install, because node_modules is never committed (the template's .gitignore excludes it). package-lock.json, on the other hand, is committed, so every teammate and the CI server install the same package versions. That file is what prevents "it works on my machine" problems caused by version drift.

To deploy, you upload the HTML, JavaScript and CSS that npm run build writes to dist/ to any static hosting service; no Node.js is needed on the server. Settings that change per environment, such as an API URL, go in a .env file under names starting with VITE_ and are read in code as import.meta.env.VITE_API_URL. Those values end up in the browser bundle, so never put secret keys there.

Common mistakes

Common mistakes

1. Running commands outside the project folder

Forget cd my-react-app after npm create and npm run dev complains that it cannot find package.json, or reports "Missing script: dev". Check which folder your terminal prompt is in first.

2. Skipping npm install

Without node_modules, npm run dev cannot find the vite command and fails with "vite: command not found" (on Windows, "'vite' is not recognized"). Run npm install after every fresh create or clone.

3. Putting JSX in a .js file

With its default settings Vite compiles JSX only in .jsx (or .tsx) files. JSX inside App.js produces a parse error that says to name the file with the .jsx or .tsx extension. Give component files the .jsx extension.

4. Following a Create React App tutorial

npx create-react-app is deprecated and pulls in outdated packages with a wall of warnings. Start new projects with Vite or a framework recommended on react.dev.

Best practices

Best practices

  • Give each project its own folder and a lowercase name without spaces, such as profile-app.
  • Keep the dev server running in one terminal tab and open a second tab for git and npm install.
  • Run npm run build and npm run preview before deploying to test the production build locally.
  • Add packages with npm install <name> so both package.json and the lock file are updated.

Mini challenge

Mini challenge: prove your setup works

Create a new project called profile-app, delete all the starter content in src/App.jsx, and replace it with a component that shows the installed React version.

  • Create the project with npm create vite@latest profile-app -- --template react and run it with npm run dev
  • App shows the project name in an <h1>
  • Import version from react and display it
  • List the three tools you used: Node.js, npm and Vite
src/App.jsx
// TODO: import { version } from "react";

function App() {
  // TODO: const projectName = ...; const tools = [...];

  return (
    <main className="setup-check">
      {/* TODO: <h1>{projectName} is running</h1> */}
      {/* TODO: show the React version */}
      {/* TODO: one <li> per tool, each with a key */}
    </main>
  );
}

export default App;

Expected: The page shows the heading "profile-app is running", "React version: 19.x.x" (whatever version you installed), a list of Node.js, npm and Vite, and the line "Edit src/App.jsx and save to see the change."

Show hint

The react package exports a string called version. Build the list with tools.map((tool) => <li key={tool}>{tool}</li>). After saving, watch the browser update without a manual refresh.

Show solution
src/App.jsx
import { version } from "react";

function App() {
  const projectName = "profile-app";
  const tools = ["Node.js", "npm", "Vite"];

  return (
    <main className="setup-check">
      <h1>{projectName} is running</h1>
      <p>React version: {version}</p>
      <ul>
        {tools.map((tool) => (
          <li key={tool}>{tool}</li>
        ))}
      </ul>
      <p>Edit src/App.jsx and save to see the change.</p>
    </main>
  );
}

export default App;

Quick quiz 1 / 3

What does npm run build do?

Quick quiz 2 / 3

Right after cloning a project, npm run dev prints "vite: command not found". What is the most likely cause?

Quick quiz 3 / 3

In a Vite project, which file first renders <App /> onto the page?

What to remember

  • Browsers understand neither JSX nor bare package imports, so you need a build tool such as Vite.
  • npm create vite@latest app -- --template react, cd app, npm install and npm run dev start a new project.
  • index.html loads src/main.jsx, and main.jsx renders <App /> into #root.
  • npm run build produces dist/; Create React App is deprecated, so do not start new projects with it.

React Versions — React

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Get Started (Vite) | Thuta Learning