This tutorial aims to help you understand how to set up a project using Vite. Vite is a modern web development build tool that significantly improves the frontend development experience. By the end of this tutorial, you will be able to create a new Vite project, understand key concepts, and apply best practices.
To follow this tutorial, you should have basic knowledge of JavaScript. Installing Node.js and npm on your machine is also required since Vite requires Node.js version 12.0.0 or higher.
Installation
Vite is available as a package on npm. You can install it globally on your machine by running the following command in your terminal:
npm install -g create-vite
Creating a New Project
After the installation, you can create a new Vite project with the following command:
create-vite my-vite-project
Here, my-vite-project
should be replaced with the name of your project.
Project Startup
Navigate into your project's directory and run the project with the following commands:
cd my-vite-project
npm run dev
You should see your application running on http://localhost:5000
.
Sample index.html
File
When you create a new Vite project, a basic index.html
file is generated:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script src="/src/main.js"></script>
</body>
</html>
The script
tag is used to link the JavaScript file main.js
to the HTML file. The div
with the id app
is where your app will be mounted.
Sample main.js
File
The main.js
file contains the JavaScript code for your application. A basic main.js
file in a Vite project may look like this:
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
Here, App.vue
is imported from the src
folder and then mounted to the div
with the id app
in the index.html
file.
In this tutorial, you learned how to set up a Vite project, run the application, and understand the basic structure of the project. To further enhance your skills, consider exploring other features of Vite, such as its fast Hot Module Replacement (HMR), pre-configured Rollup build, and out-of-the-box support for TypeScript, JSX, CSS, JSON, and more.
Exercise 1: Create a new Vite project and run the application.
Exercise 2: Modify the index.html
and main.js
files to display a simple "Hello, World!" message.
Exercise 3: Explore the Vite documentation and implement a new feature in your project.
For solutions and further practice, consider referring to the Vite documentation.
Remember, the best way to learn is by doing. So, practice as much as you can and don't be afraid to make mistakes. Happy coding!