Setup & Environment
Before you write a single line of React code, you need the right environment. This chapter walks you through installing Node.js & npm, choosing between Vite and Create React App, setting up VS Code with the best React extensions, and understanding the project folder structure that every React app follows. By the end, you'll have a fully working React development environment running locally on your machine.
localhost:5173
1. Prerequisites
Before setting up React, make sure you have the following foundational knowledge and tools. React is a JavaScript library, so comfort with basic JavaScript is important. Here's what you need coming into this chapter:
cd and running commands.
2. Install Node.js & npm
React itself runs in the browser, but your development tooling — Vite, webpack, the package manager, babel — all run on Node.js. Node.js is a JavaScript runtime for your computer (not the browser). When you install Node.js, npm (Node Package Manager) comes bundled with it automatically.
Verify your installation by running these commands in your terminal:
# In PowerShell or Command Prompt C:\Users\you> node -v v20.11.0 C:\Users\you> npm -v 10.2.4 # If "node" is not recognized, install from nodejs.org
# In Terminal (brew is optional but recommended) $ brew install node # via Homebrew (recommended) # OR download from nodejs.org directly $ node -v v20.11.0 $ npm -v 10.2.4
# Ubuntu / Debian — using NodeSource $ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - $ sudo apt-get install -y nodejs $ node -v v20.11.0 $ npm -v 10.2.4
You can also use nvm (Node Version Manager) to manage multiple Node.js versions on the same machine — very useful if you work on multiple projects requiring different Node versions.
# Install nvm (Node Version Manager)
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Restart terminal, then install Node 20 LTS
$ nvm install 20
$ nvm use 20
$ node -v # v20.x.x
3. Vite vs Create React App (CRA) — Which Should You Use?
Two popular tools scaffold a new React project for you. In 2023, the React team officially deprecated Create React App in favour of modern alternatives. Here's a direct comparison so you understand your options:
| Feature | ⚡ Vite | 🏗 Create React App |
|---|---|---|
| Status | ✅ Actively maintained | ⚠️ Officially deprecated |
| Cold start speed | ~200ms (native ESM) | 5–30s (webpack bundle) |
| Hot Module Replacement | Instant (<50ms) | Seconds (full re-bundle) |
| Bundle tool | Rollup (production) | webpack 4 |
| Config needed | Minimal (vite.config.js) | None (or eject) |
| TypeScript support | First-class, built-in | Supported (template flag) |
| Node.js requirement | ≥ Node 18 | ≥ Node 14 |
| Recommended for | All new projects ✅ | Legacy projects only |
4. Create a React App with Vite
Vite's scaffolding command creates a complete, production-ready React project in under a minute. Open your terminal, navigate to where you want to create the project, and run:
# Step 1: Create the project (choose a name)
$ npm create vite@latest my-react-app -- --template react
# Step 2: Move into the project directory
$ cd my-react-app
# Step 3: Install dependencies
$ npm install
# Step 4: Start the dev server
$ npm run dev
# Output — your app is live at:
VITE v5.x.x ready in 214 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose--template react-ts instead:
npm
create vite@latest my-app -- --template react-ts
5. Create React App (CRA) — Legacy Reference
If you encounter a project that was started with CRA — or you're learning from older tutorials — here's how CRA works. Note: the React team deprecated CRA in early 2023 and recommends migrating to Vite or a meta-framework like Next.js.
# Create a new CRA app
$ npx create-react-app my-app
# Move into directory and start dev server
$ cd my-app
$ npm start
# App opens at http://localhost:3000# Scaffold npm create vite@latest app \ -- --template react # Install npm install # Dev server npm run dev # → localhost:5173 # Build for production npm run build
# Scaffold npx create-react-app app # Dev server npm start # → localhost:3000 # Build for production npm run build
react.dev no longer
recommends CRA. Existing CRA projects should migrate to Vite. For all new projects in this
tutorial series, we use Vite.
6. VS Code Setup for React Development
Visual Studio Code is the most popular editor for React development by a large margin. With the right extensions, it becomes a powerful React IDE. Here are the extensions every React developer should install:
rafce → full component boilerplate. Hundreds
of React shortcut snippets.Ctrl+Shift+X (or
Cmd+Shift+X on Mac) to open the Extensions panel, then
search for each extension name above. All are free and come from verified publishers.
Set up format on save in VS Code settings for a seamless experience:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"emmet.includeLanguages": {
"javascript": "javascriptreact"
},
"files.associations": {
"*.js": "javascriptreact"
}
}7. Understanding the Project Folder Structure
After running npm create vite@latest, your project will
have this folder structure. Every file has a purpose — let's go through each one:
Let's look at the three most important files in detail:
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
// Creates a React root attached to the #root div in index.html
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
)import { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
function App() {
const [count, setCount] = useState(0)
return (
<div className="App">
<img src={reactLogo} alt="React logo" />
<h1>Hello React + Vite!</h1>
<button onClick={() => setCount(count + 1)}>
Count is {count}
</button>
</div>
)
}
export default App<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My React App</title>
</head>
<body>
<!-- React mounts your entire app into this div -->
<div id="root"></div>
<!-- Vite injects the bundled JS here automatically -->
<script type="module" src="/src/main.jsx"></script>
</body>
</html><div id="root"> in
index.html is the mounting point. React
takes over this div and renders your entire application inside it — the rest of the HTML is
generated entirely by React in JavaScript.
8. npm Scripts — dev, build & preview
Your package.json defines scripts that automate common
tasks. Here are all the scripts Vite gives you and exactly what they do:
{
"name": "my-react-app",
"version": "0.0.0",
"scripts": {
"dev": "vite", // starts the dev server with HMR
"build": "vite build", // builds optimized production bundle
"preview": "vite preview", // previews the production build locally
"lint": "eslint . --ext js,jsx --report-unused-disable-directives"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.2.1",
"vite": "^5.0.0"
}
}npm run devnpm run builddist/ folder. Ready for deployment.
npm run previewdist/ folder locally so you can test the final
build before deploying. Always run this after build.
npm run lint.js and
.jsx files to find and report code quality issues
before shipping.
9. Your First React App — Running Locally
Everything is set up. Let's do the complete first-run walkthrough from scratch — creating, customising, and viewing your first React component in the browser:
# 1. Scaffold project with Vite
$ npm create vite@latest hello-react -- --template react
$ cd hello-react && npm install
# 2. Replace src/App.jsx with our simple component
# (edit in VS Code — the dev server will hot-reload)
$ npm run devNow replace the contents of
src/App.jsx with this clean starting point:
import { useState } from 'react'
function App() {
const [name, setName] = useState('World')
return (
<div style={{ textAlign: 'center', padding: '40px' }}>
<h1>👋 Hello, {name}!</h1>
<p>React + Vite is running on your machine.</p>
<input
type="text"
placeholder="Type your name..."
value={name}
onChange={(e) => setName(e.target.value)}
style={{ padding: '8px 14px', fontSize: '16px', borderRadius: '8px' }}
/>
</div>
)
}
export default App
Open http://localhost:5173 in your browser. You'll see
"Hello, World!" with an input field. Type your name — the heading updates
instantly
with no page reload. That's React's reactivity in action!
useState is a React Hook that creates a reactive state
variable. When name changes, React automatically re-renders
the component and updates only the parts of the DOM that changed. We cover
useState fully in Chapter 5.
10. Explore More Tutorials
React works best with a strong foundation. These tutorials will support your React journey:
npm run build,
Node.js processes your JSX files, bundles them, and outputs plain JavaScript that the
browser understands. Node.js is the build environment, not the runtime
environment.
npm run dev and npm run
build?
npm run build creates a production-ready output in the
dist/ folder. Code is minified, tree-shaken (unused
code removed), and split into chunks. This is what you deploy to a web server or CDN.
- Cold start in ~200ms vs 15–30s for CRA
- HMR (hot reload) under 50ms vs seconds
- CRA was officially deprecated by the React team in 2023
- Vite is actively maintained and the industry standard
index.html file?
- Node.js + npm are required for React's build tooling, not for running React in the browser.
- Always use the LTS version of Node.js (18+ or 20+) for React projects.
- Vite is the modern, official choice — use it for all new projects. CRA is deprecated.
- Scaffold with
npm create vite@latest my-app -- --template react. - src/main.jsx is the entry point; it renders
<App />into the#rootdiv. - Install ES7+ Snippets, Prettier, ESLint, GitLens in VS Code — must-haves for React.
npm run dev→ development server;npm run build→ production bundle.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.