Tutorials React JS Setup & Environment
Ch 2 / 46
Chapter 2 Beginner 8 min read React JS

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.

Advertisement
What you'll learn
Node.js & npm — what they are and why React needs them
Vite vs CRA — the modern way to scaffold a React project
VS Code setup — must-have extensions for React developers
Project folder structure — understanding every file and directory
npm scripts — dev, build, preview commands explained
Running your first React app at 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:

JavaScript Basics
Variables, functions, arrays, objects, ES6+ (arrow functions, destructuring, spread). Chapter 1 assumed this foundation.
HTML & CSS
Basic HTML structure and CSS styling. React renders HTML — knowing it helps you understand JSX immediately.
Command Line
Basic terminal/command prompt usage — navigating folders with cd and running commands.
A Code Editor
We'll use Visual Studio Code (free) throughout this tutorial. We'll configure it in Section 5.
Prerequisite Check If you haven't learned JavaScript yet, complete the K2infocom JavaScript Tutorial first. It's free and covers everything React requires.

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.

① Visit nodejs.org and download the LTS version (Long Term Support — recommended for most users)
↓
② Run the installer — it installs Node.js and npm together
↓
③ Open terminal and verify with node -v and npm -v

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
Recommended Version Use Node.js 18 LTS or Node.js 20 LTS. React 18+ and Vite 5+ both require Node 18 or higher. Always prefer the LTS (Long Term Support) release over the Current/Latest release for stability.

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.

bash — install nvm (optional but recommended)
# 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
Advertisement

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
Our Recommendation Use Vite for all new React projects. It's the official recommendation from the React team, 10–100x faster than CRA, and the industry standard in 2025. We cover both below, but this tutorial primarily uses Vite.

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:

bash — scaffold a new React + Vite project
# 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
TypeScript variant Want TypeScript instead of plain JavaScript? Use --template react-ts instead: npm create vite@latest my-app -- --template react-ts
⭐⭐⭐⭐⭐
4.9 / 5 rating
🔥 Most Popular at K2infocom · Vadodara
Frontend Development
Complete Masterclass
Master the full front-end stack — HTML, CSS, JavaScript, React JS, Bootstrap & more. Learn by building 10+ real-world projects. Get certified and job-ready in 3–6 months.
HTML5 & CSS3 JavaScript ES6+ React JS Bootstrap 5 Git & GitHub 5+ Projects Certificate
2,500+ Students Trained
10+ Live Projects
3–6 Months Duration
100% Placement Support

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.

bash — Create React App (deprecated, reference only)
# 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
Vite — Modern (Recommended)
# Scaffold
npm create vite@latest app \
  -- --template react

# Install
npm install

# Dev server
npm run dev
# → localhost:5173

# Build for production
npm run build
CRA — Deprecated (Legacy)
# Scaffold
npx create-react-app app




# Dev server
npm start
# → localhost:3000

# Build for production
npm run build
CRA Deprecated The official React documentation at react.dev no longer recommends CRA. Existing CRA projects should migrate to Vite. For all new projects in this tutorial series, we use Vite.
Advertisement

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:

ES7+ React/Redux Snippets
Type rafce → full component boilerplate. Hundreds of React shortcut snippets.
Must Have
Prettier — Code Formatter
Auto-formats JSX, JSON, CSS on save. Consistent code style across your entire project.
Must Have
ESLint
Catches JavaScript/React errors and style issues in real-time as you type. Catches bugs before runtime.
Must Have
GitLens
Supercharged Git features — see blame, history, and diffs inline in your editor.
Must Have
Tailwind CSS IntelliSense
Autocomplete, linting and hover previews for Tailwind utility classes in JSX.
Recommended
Auto Import — ES6/TS
Automatically imports React components and modules as you type their names — saves huge time.
Recommended
React Developer Tools
Browser extension (Chrome/Firefox) — inspect component trees, props, state and hooks live in devtools.
Browser Extension
One Dark Pro Theme
Popular dark theme that makes JSX syntax pop with clear colour differentiation. Easy on the eyes.
Recommended
Quick Install In VS Code, press 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:

json — .vscode/settings.json
{ "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:

my-react-app — Project Explorer
my-react-app/ project root
node_modules/ never edit — all installed npm packages
public/ — static assets served as-is (no import needed)
vite.svg — favicon / static SVG icon
src/ your code here — all your React source files
assets/ — images, fonts, SVGs to import
react.svg — React logo asset
App.css — component-level styles for App
App.jsx start here — root App component
index.css — global CSS reset & base styles
main.jsx entry point — renders <App /> into #root
index.html HTML shell — contains <div id="root">
package.json — metadata, scripts, dependencies
package-lock.json — locked dependency versions (commit this)
vite.config.js config — Vite build & dev server settings
entry point
edit freely
project root / shell
config file
auto-generated (don't touch)

Let's look at the three most important files in detail:

jsx — src/main.jsx (entry point)
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> )
jsx — src/App.jsx (root component)
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
html — index.html (shell file)
<!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>
The Magic Div The <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:

json — package.json scripts block
{ "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 dev
Starts the local development server at localhost:5173. Supports Hot Module Replacement (HMR) — changes reflect instantly without a full page reload.
npm run build
Compiles and bundles your app for production. Outputs minified JS and CSS to the dist/ folder. Ready for deployment.
npm run preview
Serves the production dist/ folder locally so you can test the final build before deploying. Always run this after build.
npm run lint
Runs ESLint across all your .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:

bash + jsx — complete first run walkthrough
# 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 dev

Now replace the contents of src/App.jsx with this clean starting point:

jsx — src/App.jsx — clean first component
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!

What just happened? 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.
🚀 Try React Online — No Setup Needed
Not ready to set up locally yet? Use OneCompiler to practise React code right in your browser. Perfect for quick experiments.
Open React Playground
Advertisement
Quick Check — Test Your Understanding
Q1. Why does React need Node.js if React runs in the browser?
✅ Answer
React itself runs in the browser, but the development tooling runs on your machine via Node.js. Tools like Vite, webpack, Babel (JSX transpiler), and npm all require Node.js to execute. When you run 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.
Q2. What is the difference between npm run dev and npm run build?
✅ Answer
npm run dev starts a local development server with Hot Module Replacement (HMR). Files are served unbundled using native ES modules — fast to start, easy to debug. Source maps are included. Not optimised for production.

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.
Q3. Why is Vite preferred over Create React App in 2025?
✅ Answer
CRA uses webpack, which must bundle the entire application before the dev server starts — this is slow (5–30s). Vite uses native ES Modules in the browser, so only the files you actually request are transformed. This means:
  • 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
Multiple Choice Questions
MCQ 1
Which command starts the Vite development server for a React project?
MCQ 2
Where does React mount your app in the index.html file?
MCQ 3
Which file is the entry point of a Vite React application?
Chapter 2 Summary
  • 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 #root div.
  • 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

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel