What is Vite?
Vite is a modern frontend build tool created by Vue.js creator Evan You. It delivers exceptional cold start performance, instant hot module replacement, and native support for ES modules, with a streamlined, flexible configuration workflow that simplifies Vue 3 project setup.
Step 1: Create a New Vue 3 Project
First, confirm you have a recent Node.js version installed. You can install the Vite scaffolding tool globally, or use the npm create command without global installation:
# Global installation option
npm install -g create-vite
# Alternatively with Yarn
yarn global add create-vite
# Scaffold a new Vue 3 project
create-vite vue-vite-demo --template vue
This command creates a new project directory named vue-vite-demo using the official Vue 3 template.
Step 2: Install Project Dependencies
Navigate into the newly created project directory and install required dependencies:
cd vue-vite-demo
npm install
# Or use Yarn
yarn
Step 3: Understand the Project Structure
The generated project directory has this standard structure:
vue-vite-demo/
├── index.html # Main entry HTML file for the application
├── package.json # Lists project dependencies and run scripts
├── src/ # Core source code directory
│ ├── assets/ # Static assets such as images and fonts
│ ├── components/ # Reusable Vue single-file components
│ ├── App.vue # Root application component
│ └── main.js # Application entry point script
└── vite.config.js # Vite configuration file
Each file and directory serves a dedicated purpose:
index.html: Serves as the entry point for the browser, loading the compiled Vue applicationpackage.json: Contains metadata about the project, including dependencies and predefined run commandssrc/: Houses all application source codevite.config.js: Allows customization of Vite's behavior for the project
Step 4: Launch the Development Server
Start the local development server to preview your application:
npm run dev
# Or with Yarn
yarn dev
By default, the server runs at http://localhost:5173, where you can view your live Vue 3 application.
Step 5: Review Core Example Code
Application Entry Point (src/main.js)
import { createApp } from 'vue'
import RootApp from './App.vue'
createApp(RootApp).mount('#app')
This script imports Vue's createApp API, imports the root appplication component, and mounts the app to the DOM element with ID app.
Root Component (src/App.vue)
<template>
<div id="root-app">
<img alt="Vue Logo" src="./assets/vue-logo.svg">
<GreetingCard message="Welcome to Your Vue 3 + Vite App"/>
</div>
</template>
<script>
import GreetingCard from './components/HelloWorld.vue'
export default {
name: 'RootApp',
components: {
GreetingCard
}
}
</script>
<style>
#root-app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
This root component imports and uses a reusable greeting component, with a template, script section, and global styles.
Step 6: Customize Vite Configuration
Edit vite.config.js to adjust Vite's behavior. For example, add a path alias to simplify imports:
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import { resolve } from 'node:path'
export default defineConfig({
plugins: [vuePlugin()],
resolve: {
alias: {
'@': resolve(__dirname, './src')
}
}
})
This configuration adds a alias @ that points directly to the src directory, letting you import files using concise paths like @/components/GreetingCard.vue instead of long relative paths.
Step 7: Generate Production Build
Once development is complete, run the production build command to create optimized static assets:
npm run build
# Or with Yarn
yarn build
This command compiles and minifies the application code, outputting the final production-ready files to the dist directory.