Creating an App with Vite
npm create vite@latest app-name -- --template react
cd app-name
npm install
npm run dev
Why Vite Is Popular
Vite is a build tool that starts a dev server almost instantly and updates the browser in milliseconds when you save a file, because it serves your source files directly over native ES modules instead of bundling everything up front the way older tools do.
Example: Why Vite Is Popular
$ npm create vite@latest my-app -- --template react
$ npm run dev
ready in 180ms
⚠️ Run this in your own terminal or Node.js environment.
Vite's Project Structure
A Vite + React project has an index.html at the project root (not inside public/, unlike CRA), a src/ folder with main.jsx as the entry point, and a vite.config.js file for build settings.
Example: Vite's Project Structure
$ ls -R my-app
my-app:
index.html node_modules package.json public src vite.config.js
my-app/public:
vite.svg
my-app/src:
App.css App.jsx assets index.css main.jsx
⚠️ Run this in your own terminal or Node.js environment.
Running and Building with Vite
npm run dev starts Vite's development server; npm run build creates an optimized production bundle in a dist/ folder, ready to deploy.
Example: Running and Building with Vite
npm run dev # local dev server
npm run build # bundles into dist/
⚠️ Run this in your own terminal or Node.js environment.
- Forgetting to select the React template when running
npm create vite@latest. - Expecting the exact same file structure as Create React App -- Vite's defaults differ slightly.
- Not running
npm installbeforenpm run devafter cloning a Vite project.
- Vite is a fast, modern build tool that's now the recommended way to start a React project.
- It uses native ES modules during development for near-instant server start and hot reload.
npm create vite@latestscaffolds a new project with a template picker.npm run devstarts the dev server;npm run buildcreates the production bundle.
The generated app supports all modern evergreen browsers; Vite's dev server itself relies on native ES module support in the browser during development only.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: