Introduction to Vite and Installation

Learn what Vite is, why it is faster than traditional bundlers, and how to create and run your first Vite project.

🎯 Introduction

By the end of this article, you will understand what Vite is, the problems it solves, and why it is a common choice for new Vue 3 projects. You will also create a Vite project with a single command, start the development server, and run a production build.


🧱 What Is Vite?

Vite is a next-generation frontend build tool, led by Vue creator Evan You. Its name is pronounced /vit/ and means “fast” in French.

Traditional bundlers such as Webpack bundle all files into one or more bundles during development. As a project grows, startup becomes slower. Vite takes a different approach:

  • During development, it serves files on demand using the browser's native ES module support.
  • After a code change, it updates only the affected modules, providing extremely fast hot module replacement (HMR).
  • For production builds, it uses Rollup to optimize the output.
Think of Vite as a convenience store during development and a factory during production builds: it processes each module as you need it during development, without bundling everything first, so startup is fast. For production, it bundles, minifies, and splits the code together, producing smaller assets that load faster.

⚡ Why Choose Vite?

ComparisonTraditional bundlersVite
Development startupBundle before starting; slower as the project growsStarts immediately, in milliseconds
HMRRecompile the whole bundle or many modulesUpdate only modified modules
Configuration complexityMany options and a steep learning curveWorks out of the box with simple configuration
Production buildsBuilt in or provided by pluginsEfficient output powered by Rollup
Vite is not a complete replacement for Webpack. Webpack still has advantages for very large projects and specialized loader ecosystems. For most new Vue 3 projects, however, Vite offers a better development experience.

🚀 Install Vite and Create Your First Project

Vite provides an official scaffolding tool. Use create-vite to create a project:

npm create vite@latest

The command prompts you to choose:

  1. A project name, such as my-vite-app.
  2. A framework, such as Vue or Vanilla.
  3. A language variant, such as JavaScript or TypeScript.

You can also provide the options in a single command:

npm create vite@latest my-vite-app -- --template vue
In -- --template vue, -- passes the remaining arguments to create-vite. You can replace vue with vue-ts or another template.

Enter the project directory and install dependencies:

cd my-vite-app
npm install

🛠 Common Commands

Once installation is complete, package.json already contains several scripts:

package.json
{
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "preview": "vite preview"
    }
}

Start the development server:

npm run dev

By default, the site is available at http://localhost:5173/, where you can view the starter page.

Create a production build:

npm run build

This command generates a dist directory in the project root containing the static assets you can deploy to a server.

Preview the production build locally:

npm run preview
preview starts a local server that serves the contents of dist, allowing you to check the final result before deployment.

❓ Questions and Answers

Q1: Is Vite only for Vue projects?

No. Vite officially supports Vue, Preact, Lit, Svelte, Solid, and vanilla JavaScript.

Q2: What is the main difference between Vite and Webpack?

During development, Vite lets the browser request ES modules directly without bundling them first. Webpack completes the bundle before serving the application.

Q3: Why does Vite start so quickly?

It skips development-time bundling and uses native browser modules, processing each module only when it is needed.

Q4: Does production also use ES modules?

For production builds, Vite uses Rollup to bundle, minify, and split the code into static files with broader browser compatibility.


🧪 Exercise

Create a Vite + Vue project by following these steps:

  1. Run npm create vite@latest and select Vue and JavaScript.
  2. Install dependencies and run npm run dev.
  3. Open src/App.vue and replace the heading with your name.
  4. Run npm run build and inspect the generated dist directory.
src/App.vue
<template>
    <h1>Change this heading</h1>
</template>

🎉 You have completed the introduction to Vite! In the next article, we will explore common vite.config.js options to tailor your project.