First Steps with Vite

Tutorial 4 of 5

First Steps with Vite

1. Introduction

Tutorial's Goal

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.

What You Will Learn

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

Prerequisites

  • Basic understanding of JavaScript and Node.js
  • Node.js (version 12 or above) and npm installed on your computer

2. Step-by-Step Guide

Installation of Vite

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.

Creating a New Project

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.

Starting Your Development Server

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.

3. Code Examples

Basic index.html Structure

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>

Importing a JavaScript File

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.

4. Summary

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.

5. Practice Exercises

Exercise 1

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.

Exercise 2

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.

Exercise 3

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.