This tutorial will guide you through your first experience with Vite. You'll learn how to install Vite, create a new project, and start your development server.
By the end of this tutorial, you will:
- Understand what Vite is and how it works
- Be able to install Vite
- Create a new Vite project
- Start and run your Vite development server
Vite (French word for "fast") is a modern front-end build tool created by Evan You, the creator of Vue.js. It provides a faster and leaner development experience for modern web projects.
To install it globally, open your terminal and enter the following command:
npm install -g create-vite
Tip: If you're facing permission issues, try using sudo
before the command.
After successfully installing Vite, you can create a new project by running:
create-vite my-vite-project
Replace my-vite-project
with the name of your project. This command creates a new directory with your project's name, sets up the initial project structure, and installs the necessary dependencies.
Navigate to your newly created project directory:
cd my-vite-project
Then, start the development server:
npm run dev
You should see output informing you that the server is running, along with the local address to view your application in the browser.
In your project's root directory, you will find the index.html
file. This is the main HTML file of your application. A basic structure of index.html
looks like this:
<!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">
<!-- Your application will be rendered here -->
</body>
</html>
In the src
directory, create a new file named main.js
. You can import this file in your index.html
like this:
<script type="module" src="/src/main.js"></script>
Your main.js
file could look something like this:
console.log('Hello, Vite!')
When you run your development server with npm run dev
, you should see 'Hello, Vite!' in the browser's console.
In this tutorial, you've learned what Vite is, how to install it, how to create a new project, and how to start your development server. The next steps could be learning about how to use Vite with frameworks like Vue.js, React, and others.
Create a new Vite project and start the development server. Try to modify the main.js
file and observe the changes in the browser without refreshing.
Try to integrate CSS and images in your project. You can create a CSS file in the src
directory and import it in your main.js
file.
Explore the Vite configuration file vite.config.js
. Try to change the server's port and start the development server again.
Keep practicing, and don't forget to check the Vite documentation for more information and guidance.