Introduction to Vite and Installation
🎯 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.
⚡ Why Choose Vite?
| Comparison | Traditional bundlers | Vite |
|---|---|---|
| Development startup | Bundle before starting; slower as the project grows | Starts immediately, in milliseconds |
| HMR | Recompile the whole bundle or many modules | Update only modified modules |
| Configuration complexity | Many options and a steep learning curve | Works out of the box with simple configuration |
| Production builds | Built in or provided by plugins | Efficient output powered by Rollup |
🚀 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:
- A project name, such as
my-vite-app. - A framework, such as
VueorVanilla. - A language variant, such as
JavaScriptorTypeScript.
You can also provide the options in a single command:
npm create vite@latest my-vite-app -- --template vue
-- --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:
{
"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:
- Run
npm create vite@latestand select Vue and JavaScript. - Install dependencies and run
npm run dev. - Open
src/App.vueand replace the heading with your name. - Run
npm run buildand inspect the generateddistdirectory.
<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.
Composition, TypeScript, and Plugins
Learn to organize multiple Pinia stores, use TypeScript, and extend stores with plugins for features such as state persistence.
Basic Configuration
Learn common vite.config.js options, including path aliases, server ports, build output directories, and plugin registration.
