Chuyển đến nội dung

Bài 2 · Getting Started

Create Your First Vite Project

Scaffold a Vite project with npm create vite, pick a template, install dependencies, start the dev server and learn what each generated file does.

The fastest way to start with Vite is the official scaffolding tool, create-vite. One command asks a few questions, copies a template into a new folder and leaves you with a working app. In this lesson you will create a project, run it, change something, and understand every file you get.

Vite is not installed on this site, so the terminal commands are read-only. Follow along on your own computer. You need Node.js: Vite 7 requires Node 20.19 or newer (or 22.12+). Check yours with node --version.

Run the scaffolder

Open a terminal in the folder where you keep projects and run one of these, depending on your package manager:

Terminal
npm create vite@latest my-app
# or
pnpm create vite my-app
yarn create vite my-app
bun create vite my-app

npm create vite is shorthand for running the create-vite package. Without a name it asks for one. You then choose:

  1. A framework: Vanilla, Vue, React, Preact, Lit, Svelte, Solid, Qwik or Others.
  2. A variant: for example JavaScript or TypeScript (and for React, optionally with SWC, a faster compiler).

You can skip the questions by naming the template:

Terminal
npm create vite@latest my-app -- --template vanilla-ts
npm create vite@latest my-react-app -- --template react-ts

The extra -- after the package name passes the flag through npm to create-vite. Other package managers do not need it.

Install and start

Terminal
cd my-app
npm install
npm run dev

The terminal prints something like this:

TEXT
  VITE v7.x.x  ready in 312 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose

Open http://localhost:5173/. Port 5173 is Vite's default (a leetspeak spelling of "VITE": V=5, I=1, T=7, E=3). If the port is busy, Vite uses the next free one and says so.

What was generated

A vanilla template gives you a layout like this:

TEXT
my-app/
├─ index.html
├─ package.json
├─ public/
│  └─ vite.svg
└─ src/
   ├─ main.js
   ├─ counter.js
   ├─ style.css
   └─ javascript.svg

The structure is explained fully in Project Structure, but the key points are:

  • index.html is at the project root. In Vite it is the entry point, not a file buried in a public folder. It contains a script tag that loads your code.
  • src/main.js is the JavaScript entry that the HTML loads.
  • public/ holds files that are copied as-is (see Static Assets and the public Directory).
  • package.json lists the three scripts you will use every day.
JSON
{
  "name": "my-app",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "devDependencies": {
    "vite": "^7.0.0"
  }
}

Notice "type": "module". It tells Node that .js files in the project are ES modules, which is how Vite's own config file is read. Vite is a dev dependency: it is a tool you run, not code that ships to users.

The heart of it: index.html

Open index.html in a Vite project and you will see a normal page with one special line:

HTML
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>My App</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

type="module" makes the browser treat main.js as an ES module, so it can use import. The path starts with /, which Vite maps to the project root. During vite build, Vite reads this HTML, finds the script, follows its imports, and rewrites the tag to point to a hashed bundle.

You can try the browser side of this idea right now. The inline module below behaves like main.js would, using an import-style structure inside one file:

An entry module that renders into #app

Scaffolding into the current folder

Use . as the name to create the files in the folder you are already in:

Terminal
npm create vite@latest .

If the folder is not empty, create-vite asks whether to remove existing files, ignore them, or cancel. Read the prompt before answering.

A scaffolder in miniature

What create-vite does is not complicated: it copies a template folder and edits package.json. This runnable stand-in builds the same file list in memory so you can see the idea. It is not the real tool.

Node.js Stand-in: what create-vite does

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Community templates and other starters

create-vite ships the official templates. You can also scaffold from a GitHub repository using degit-style names, for example npm create vite@latest my-app -- --template ... with a community template, or use a framework's own tool, such as npm create svelte@latest or npm create vue@latest, which are built on Vite and ask more questions (router, testing, linting).

Common beginner mistakes

  • Opening index.html by double-clicking it. The file path starts with file://, so module imports and root-relative paths fail. Always use the dev server.
  • Forgetting npm install. Without it, npm run dev says vite: command not found.
  • Using an old Node version. The error often mentions an unsupported syntax. Upgrade Node.
  • Editing files in dist/. That folder is generated by the build; changes are overwritten.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which command scaffolds a new Vite project?

    npm create vite runs the create-vite package, which asks for a framework and variant and copies a template.
  2. 2. Where does the entry index.html live in a Vite project?

    Vite treats the root index.html as the entry point and reads its script tags to find your code.
  3. 3. What does the script type="module" attribute allow?

    Module scripts are loaded as ES modules, so they can import other files.
  4. 4. Why shouldn't you open index.html with a double-click?

    Vite relies on being served over http. A file:// URL breaks module loading and paths like /src/main.js.

Summary

  • npm create vite@latest <name> scaffolds a project; choose a framework and variant or pass --template.
  • Run npm install, then npm run dev; the server listens on port 5173 by default.
  • The project root index.html is the entry; it loads your code with <script type="module">.
  • package.json has dev, build and preview scripts, and Vite is a dev dependency.
  • Always use the dev server instead of opening files directly.

Next: CLI Commands and npm Scripts

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.