React Installation
Node.js, npm, Create React App, and Vite — everything you need to set up React
Table of Contents
- 1. Introduction: What You Need Before React
- 2. What is Node.js and Why Do You Need It?
- 3. How to Install Node.js on Windows, Mac, and Linux
- 4. What is npm? Understanding the Node Package Manager
- 5. Essential npm Commands Every React Developer Must Know
- 6. What is Create React App (CRA)?
- 6.1 How to Create a React App with CRA
- 6.2 Folder Structure of CRA
- 6.3 Running Your First React App
- 6.4 Disadvantages of CRA (Why It's Being Deprecated)
- 7. What is Vite? The Modern Alternative
- 8. CRA vs Vite: A Detailed Comparison Table
- 9. Common Installation Errors and Fixes
- 10. Summary Cheat Sheet
1. Introduction: What You Need Before React
Before you can install or use React, you need two things on your computer:
| Requirement | Why? |
|---|---|
| Node.js | Provides the JavaScript runtime to run React build tools outside the browser. |
| npm (or yarn/pnpm) | Package manager to download React and thousands of other JavaScript libraries. |
| A Code Editor | VS Code (recommended), Sublime Text, or WebStorm. |
No Node.js? You cannot run React. React code needs to be compiled/transformed before it runs in a browser.
2. What is Node.js and Why Do You Need It?
Node.js is a JavaScript runtime built on Chrome's V8 engine that allows you to run JavaScript on your server/computer (outside a web browser).
Why does React need Node.js?
| Use Case | Explanation |
|---|---|
| Running build tools | React uses tools like Webpack (via CRA) or Vite to bundle, transpile (JSX → JS), and optimize code. These tools run on Node.js. |
| Managing dependencies | npm (which comes with Node) downloads and installs React and other libraries. |
| Running development server | When you run npm start or npm run dev, a Node.js server powers your localhost. |
Check if Node.js is already installed
Open your terminal (Command Prompt, PowerShell, or Terminal) and type:
- If you see a version number like
v20.10.0→ Node is installed. - If you see
'node' is not recognized→ You need to install it.
3. How to Install Node.js on Windows, Mac, and Linux
For Windows & Mac (Easiest Method)
- Go to the official Node.js website: https://nodejs.org
- You'll see two versions:
- LTS (Long Term Support) – Recommended for most users. Stable and reliable.
- Current – Latest features, but may have bugs.
- Click the LTS button to download the installer (.msi for Windows, .pkg for Mac).
- Run the downloaded file and follow the installation wizard.
- Accept the license agreement.
- Keep the default installation path.
- Important: Make sure the option to install npm is checked (it is by default).
- After installation, restart your terminal.
Verify installation:
For Linux (Ubuntu/Debian)
Alternative: Using Node Version Manager (nvm) – Advanced
nvm lets you install and switch between multiple Node versions easily.
4. What is npm? Understanding the Node Package Manager
npm (Node Package Manager) comes automatically with Node.js. It is:
- The world's largest software registry (over 2 million packages).
- A command-line tool to install, update, and manage JavaScript libraries.
Key Concepts
| Term | Meaning |
|---|---|
| Package | A reusable piece of code (library) like React, Express, or Lodash. |
| Dependency | A package your project needs to work. |
| package.json | A manifest file that lists all your project's dependencies and scripts. |
| node_modules | A folder where npm downloads all the packages. (Never share this folder; it's huge.) |
| package-lock.json | Locks exact versions of every dependency for consistent installs. |
5. Essential npm Commands Every React Developer Must Know
| Command | What it does |
|---|---|
npm init | Creates a new package.json file (interactive). |
npm init -y | Creates package.json with default values (faster). |
npm install <package-name> | Installs a package and adds it to dependencies. |
npm install <package-name> --save-dev | Installs as a dev dependency (needed only during development). |
npm install | Installs ALL dependencies listed in package.json. |
npm uninstall <package-name> | Removes a package. |
npm update | Updates all packages to latest versions within version rules. |
npm run <script-name> | Runs a custom script defined in package.json. |
npm list | Shows all installed packages. |
npm outdated | Shows packages that have newer versions available. |
Global vs Local Installation
- Local (default): Installs inside your project's
node_modules. Use for React. - Global (use
-gflag): Installs system-wide. Example:npm install -g vite
6. What is Create React App (CRA)?
Create React App (CRA) is an officially supported toolchain from Meta (Facebook) to set up a React project with zero configuration.
- Released: 2016
- Status: Deprecated as of 2023 (no longer recommended for new projects)
- Why it existed: Made React accessible to beginners by hiding complex Webpack/Babel configs.
6.1 How to Create a React App with CRA
Prerequisite: Node.js version 14 or higher.
Replace my-first-app with your project name (lowercase, no spaces, use hyphens).
What happens?
- A new folder
my-first-appis created. package.jsonis generated with React as a dependency.- All necessary files (Webpack, Babel, ESLint) are configured automatically.
- ~250MB of
node_modulesare downloaded.
6.2 Folder Structure of CRA
6.3 Running Your First React App
- Your app opens at
http://localhost:3000 - The page auto-reloads when you save changes (Hot Reload).
To build for production:
Creates an optimized build/ folder ready to deploy.
6.4 Disadvantages of CRA (Why It's Being Deprecated)
| Problem | Explanation |
|---|---|
| Very slow | Development server takes 10–30 seconds to start for medium/large apps. |
| Bloated | Installs 200–300MB of dependencies even for a "Hello World" app. |
| Outdated dependencies | Uses older Webpack versions; upgrading is painful. |
| No built-in TypeScript (without extra config) | Requires --template typescript flag. |
| Limited customization | To change Webpack config, you must "eject" (irreversible). |
| Officially deprecated | Meta no longer recommends CRA. The React docs now suggest frameworks like Next.js or Vite. |
Bottom line: If you are starting a new React project today, do not use Create React App. Use Vite instead.
7. What is Vite? The Modern Alternative
Vite (French for "fast") is a next-generation build tool created by Evan You (creator of Vue.js). It is now the recommended way to start a React project by the official React documentation.
Why Vite is better than CRA
| Feature | Vite |
|---|---|
| Speed | Instant server start (less than 1 second). |
| Hot Module Replacement (HMR) | Blazing fast, preserves state on updates. |
| Bundle size | Smaller, optimized production builds. |
| Config | Minimal config, but easily extensible. |
| TypeScript support | Built-in, zero config. |
| Modern defaults | Uses native ES modules, supports CSS modules, etc. |
7.1 How to Create a React App with Vite
Prerequisite: Node.js version 18 or higher.
Other templates available: react, react-ts, react-swc
Step-by-step walkthrough:
Your app runs at http://localhost:5173 (Vite uses port 5173 by default).
7.2 Folder Structure of Vite React App
Key difference from CRA: index.html is at the root level, not inside public/. This gives you more control.
7.3 Running Your Vite React App
| Command | Purpose |
|---|---|
npm run dev | Starts development server with hot reload |
npm run build | Creates production build in dist/ folder |
npm run preview | Locally preview the production build |
8. CRA vs Vite: A Detailed Comparison Table
| Feature | Create React App (CRA) | Vite |
|---|---|---|
| Official Status | Deprecated (no longer recommended) | ✅ Actively maintained & recommended |
| Server Start Time | 10–30 seconds | < 1 second |
| HMR Speed | Slow (rebundles on changes) | Instant (native ES modules) |
| Production Build Time | 30–60 seconds | 5–10 seconds |
| Bundle Size | Larger (~150KB gzipped for empty app) | Smaller (~120KB gzipped for empty app) |
| Configuration | Hidden (eject needed for changes) | Simple vite.config.js |
| TypeScript Support | Requires --template typescript | Built-in, zero config |
| CSS Modules | Yes | Yes (better performance) |
| Environment Variables | REACT_APP_ prefix required | VITE_ prefix required |
| Default Port | 3000 | 5173 |
| Learning Curve | Low | Low (very similar) |
| Recommended for New Projects | ❌ No | ✅ Yes |
When would you still use CRA?
- Maintaining an existing legacy project that already uses CRA.
- A tutorial or course that hasn't updated to Vite yet (but you should adapt it).
9. Common Installation Errors and Fixes
| Error Message | Cause | Solution |
|---|---|---|
| 'node' is not recognized | Node.js not installed or not in PATH | Install Node.js and restart terminal. |
| npm: command not found | npm not installed (rare if Node is installed) | Reinstall Node.js. |
| create-react-app is not recognized | Using outdated global install | Use npx create-react-app instead of create-react-app. |
| ERESOLVE unable to resolve dependency tree | npm version 7+ stricter dependency resolution | Run npm install --legacy-peer-deps or use --force |
| Port 3000 already in use | Another app is using the default port | Kill the process, or use a different port: PORT=3001 npm start (Linux/Mac) or set PORT=3001 && npm start (Windows) |
| npm ERR! network | Internet connection or proxy issues | Check internet. If behind proxy: npm config set proxy http://proxy.company.com:8080 |
| Error: EPERM: operation not permitted | Permission issues (Windows common) | Run terminal as Administrator, or clear npm cache: npm cache clean --force |
10. Summary Cheat Sheet
Quick Decision Guide
Question: You want to start a new React project today?
| Your situation | Recommendation |
|---|---|
| New to React | ✅ Use Vite (faster, simpler, modern) |
| Following an old tutorial | 🔄 Adapt the tutorial to Vite (same code, different setup commands) |
| Working on a team with existing CRA project | ❌ Stick with CRA for that project |
| Building a production app | ✅ Use Vite + React |
| Need server-side rendering (SSR) | ➡️ Use Next.js (not covered here) |
React Installation MCQ Practice
10 Basic MCQs 10 Advanced MCQs10 Basic React Installation MCQs
Why is Node.js required before installing React?
Which command prints your installed Node.js version?
node --version (or node -v) to verify Node.js.What does npm stand for?
Which command installs project dependencies from package.json?
npm install downloads dependencies listed in package.json.To scaffold a new React app with Vite you typically run:
npm create vite@latest is the modern recommended scaffold command.Create React App (CRA) is started with:
npx create-react-app, though Vite is now preferred for new projects.In a Vite React project, the browser entry file is usually:
src/main.jsx (or .tsx).Which file lists your React project dependencies?
package.json declares dependencies like react and react-dom.After cloning a React repo, the first command you usually run is:
npm run dev or npm start.Which port does Vite's dev server use by default?
10 Advanced React Installation MCQs
Why is CRA often discouraged for new projects in 2024+?
What does npm run dev do in a Vite React project?
npx vs npm install — npx is best for:
npx create-react-app runs the tool once without a global install.package-lock.json primarily ensures:
Setting "type": "module" in package.json means:
Which environment variable file is loaded by many React tools for local secrets?
.env.local holds local overrides (never commit secrets).If npm install fails with EACCES on Linux/Mac, a common fix is:
Vite's import.meta.env.VITE_* variables are:
Compared to CRA, Vite dev server speed comes mainly from:
To add React to an existing Vite vanilla project you install:
react and react-dom.15 React Installation Interview Questions & Answers
Easy Medium Hardnpm create vite@latest, choose React template, then npm install and npm run dev.npm install in project root; ensure react-scripts is listed in package.json dependencies.dist/ for Vite or build/ for CRA).node -v and npm -v in terminal.