Creating Your First React Application
Creating your first React application means generating a project with a build tool that sets up React, a fast development server and a production build command for you. Today the usual choice for learning is Vite, which creates a working React app in seconds. Older tutorials use Create React App, which the React team has since deprecated for new projects.
Before you start
You need Node.js installed, which also gives you npm, the package manager. Check both from a terminal with node -v and npm -v. A code editor such as VS Code and a modern browser complete the setup.
Generating the project
# Create a new React project called my-first-app
npm create vite@latest my-first-app -- --template react
# Move into it and install dependencies
cd my-first-app
npm install
# Start the development server
npm run dev
# Local: http://localhost:5173/
# Later: create an optimised build in the dist/ folder
npm run build
The extra -- passes the template option through npm to the Vite tool. Choose react-ts instead of react if you want TypeScript.
What you get
Open the project folder in your editor and you will find a small set of files:
index.htmlsits at the project root and contains a singledivwith the id "root", where React draws the app.src/main.jsxis the entry point. It finds that div and renders theAppcomponent into it.src/App.jsxis your first component, showing a counter button.package.jsonlists dependencies and thedev,buildandpreviewscripts.node_modulesholds installed packages; it is never edited by hand and is excluded from version control.
Making your first change
With the dev server running, replace the contents of App.jsx with a component of your own and save. The browser updates instantly without a full reload, thanks to hot module replacement, and component state is often preserved.
// src/App.jsx
import { useState } from "react";
function App() {
const [name, setName] = useState("");
return (
<main>
<h2>Hello{name ? `, ${name}` : ""}!</h2>
<input
placeholder="Type your name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</main>
);
}
export default App;
Problems first-timers run into
"npm: command not found" or "'npm' is not recognized" means Node.js is not installed, or the terminal was opened before installation finished. Reopen the terminal after installing.
Running npm run dev from the wrong folder gives "Missing script: dev" or "Could not read package.json". Always cd into the project folder first; the folder containing package.json is the one that matters.
Some learners also try to open index.html by double-clicking it. That shows a blank page, because the JSX has to be processed by the dev server. Always use the URL the server prints.
Finally, if port 5173 is already in use, Vite picks the next free port automatically. Read the terminal output for the exact address instead of assuming.
Stopping and restarting
Press Ctrl + C in the terminal to stop the dev server. Run npm run dev again whenever you return to the project. You only need npm install again when dependencies change or after cloning the project on another machine.
Takeaway: install Node.js, generate a project with npm create vite@latest, run npm install and npm run dev inside the project folder, and edit src/App.jsx to see changes live.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.