TechLearn Hub

Vite: A Beginner's Guide

Learn how to create, develop, build and deploy modern frontend applications using Vite.

Vite is a modern frontend build tool that provides a fast development server and a production build system. It helps developers work with JavaScript, CSS, HTML and popular frameworks such as React, Vue and Svelte.

In simple terms: Vite helps you run a web project while developing it, see changes quickly, and prepare optimized files for publishing your website.

1. What Is Vite?

Vite is a frontend development tool that provides a development server and a production build command. It is designed to make modern web development faster and more convenient.

Vite was created by Evan You, the creator of Vue.js. It can be used with plain JavaScript or with frameworks such as React, Vue and Svelte.

During development, Vite uses native browser ES modules where possible and transforms source files as needed. For production, it bundles and optimizes your application into deployable assets.

Remember: Vite is a build tool, not a programming language and not a frontend framework by itself.

2. Why Was Vite Created?

Traditional frontend projects can become slow to start as the application and its dependencies grow. Older workflows often bundle large amounts of code before the development server is ready.

Vite improves the development experience by serving source code efficiently and updating changed modules quickly. Its production build process prepares files for deployment.

  • Fast development server startup in many projects.
  • Quick feedback when source files change.
  • Built-in support for common frontend development features.
  • Project templates for popular frameworks.
  • Production builds and plugin support.

3. How Does Vite Work?

Vite has two main workflows: development and production.

Development mode

When you run the development command, Vite starts a local server. Your browser requests the application files, and Vite serves or transforms them as needed. It also provides Hot Module Replacement for supported updates.

Production mode

When you build the project, Vite processes the application and generates optimized files, normally inside a folder named dist.

Development:
npm run dev

Production build:
npm run build

The development server is intended for working on the project. A production build is normally used for publishing the finished application.

4. What Is Hot Module Replacement?

Hot Module Replacement, commonly called HMR, allows compatible modules to update in the browser without a full page reload in many situations.

For example, when you change a CSS rule, Vite can send the update to the browser so that you can see the new styling quickly.

Beginner tip: HMR makes it easier to experiment with layouts, colors and application logic because you can see many changes almost immediately.

5. Vite and JavaScript Modules

Modern browsers support JavaScript modules. A simple HTML page can load a module using a script element with the type="module" attribute.

<script type="module" src="/src/main.js"></script>

Vite builds on this module system and provides development-time transformations, dependency handling and a production build process. It can also process CSS and imported assets.

6. Installing Vite

For normal local development, you need Node.js and a package manager such as npm. npm is included with Node.js.

Check whether Node.js and npm are available by running:

node -v
npm -v

To create a new Vite project, use:

npm create vite@latest

Follow the prompts to select a project name, framework and template. The available choices may change between Vite releases.

Version note: Check the official Vite documentation for the current Node.js version requirements before installing. Requirements can change with new major releases.

7. Create Your First Vite Project

Follow these steps in a terminal on a computer or development environment that supports Node.js.

Step 1: Create a project

npm create vite@latest my-first-app

Step 2: Enter the project folder

cd my-first-app

Step 3: Install dependencies

npm install

Step 4: Start the development server

npm run dev

Vite will print a local address in the terminal. Open that address in your browser to view the application.

The default address is often http://localhost:5173, but the port can differ if that port is already being used or if you have configured another port.

You do not need to install Vite globally for this workflow. The project creation command and the project's local dependencies handle the usual setup.

8. Creating a React Project with Vite

To create a React project using the JavaScript template, run:

npm create vite@latest my-react-app -- --template react

Then install dependencies and start the development server:

cd my-react-app
npm install
npm run dev

For a React project with TypeScript, use the react-ts template:

npm create vite@latest my-react-app -- --template react-ts

Vite handles the development server and build process. React is the UI library used to build the interface.

9. Vite with Vue and Svelte

Vue

You can create a Vue project using:

npm create vite@latest my-vue-app -- --template vue
cd my-vue-app
npm install
npm run dev

Svelte

You can also select a Svelte template through the interactive project creation command. If you prefer a template name, check the current Vite documentation for the supported Svelte options.

Vite is not limited to a single framework. The appropriate template and plugins depend on the technology you choose.

10. Understanding a Vite Project Structure

A basic Vite project may contain files and folders like these:

my-first-app/
├── node_modules/
├── public/
├── src/
│   ├── assets/
│   ├── App.jsx
│   └── main.jsx
├── index.html
├── package.json
├── package-lock.json
├── vite.config.js
└── README.md

The structure depends on the selected template. A plain JavaScript project may use main.js, while a React project commonly uses JSX files.

  • src/ contains application source code in many templates.
  • public/ contains files served as-is from the public root.
  • index.html is an important HTML entry point.
  • package.json lists project scripts and dependencies.
  • node_modules/ contains installed packages.
  • vite.config.js can contain custom Vite configuration.
  • dist/ is normally generated when you build for production.
The vite.config.js file is optional for simple projects. Vite can use sensible defaults without a custom configuration file.

11. Why Is index.html Important?

In a standard Vite application, index.html is part of the application entry point. Vite processes the HTML and follows module scripts and other resources referenced from it.

A simple HTML entry point may look like this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
  <title>My Vite App</title>
</head>
<body>
  <div id="app"></div>
  <script type="module" src="/src/main.js"></script>
</body>
</html>

The element with the ID app is commonly used as a mounting point by frontend frameworks, depending on the selected template.

12. Understanding package.json

The package.json file describes the project, its dependencies and scripts. A typical Vite project includes commands similar to these:

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }
}

The actual file can contain more fields and dependencies. When you run npm run dev, npm executes the script named dev.

13. Important Vite and npm Commands

CommandPurpose
npm create vite@latestCreate a new Vite project.
npm installInstall the project's dependencies.
npm run devStart the development server.
npm run buildCreate a production build.
npm run previewPreview the production build locally.
npx viteRun the locally installed Vite CLI when available.

npm run dev

Use this command while building and testing your application. The terminal displays the local development URL.

npm run build

This runs the build script, normally vite build, to generate production files.

npm run preview

This serves the production build locally so you can check it before deployment. It is a preview tool, not a replacement for your production hosting service.

14. Vite Configuration

You can customize Vite using a configuration file in your project root, commonly named vite.config.js.

For example, to request port 3000 for the development server:

import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    port: 3000
  }
})

If the requested port is already occupied, Vite may use another port unless you configure it to fail instead.

Changing the base path

If you publish your application inside a subdirectory, you may need to set the base option.

import { defineConfig } from 'vite'

export default defineConfig({
  base: '/my-app/'
})

Use the correct base path for the location where your application will be hosted. A wrong value can cause CSS, JavaScript and image URLs to fail after deployment.

15. Vite Plugins and Dependencies

Plugins extend Vite's functionality. They can provide framework integration, transformations and additional development or build features.

For example, a React project can use the official React plugin. Many templates already configure the required plugin for you.

To add the React plugin to a project where it is needed, you can install it as a development dependency:

npm install -D @vitejs/plugin-react

Installation alone does not automatically configure every plugin. Follow the plugin's documentation to add it to your Vite configuration when required.

Dependency pre-bundling

Projects can depend on many packages from npm. During development, Vite analyzes and pre-bundles dependencies where appropriate to make them work efficiently with the development server.

This behavior is mainly a development optimization. The production build has its own processing workflow.

16. Vite and CSS

Vite supports CSS imports from JavaScript and framework files. For example:

import './style.css'

When the application is built, Vite processes CSS according to the project and its configuration. You can also add CSS preprocessors or other styling tools when needed.

17. Vite and Assets

Images, fonts and other files can be included in a Vite application. Imported assets can be processed as part of the module graph, and their URLs may be rewritten for the production build.

Files placed in the public directory are generally copied to the output as-is, while imported assets can be processed and renamed as part of the build.

Choose the approach that best suits how each asset needs to be referenced.

18. Vite Environment Variables

Vite can expose selected environment variables to client-side code. By default, variables intended for this purpose commonly use the VITE_ prefix.

For example, an environment file may contain:

VITE_API_URL=https://example.com/api

Application code can access the value through:

const apiUrl = import.meta.env.VITE_API_URL

console.log(apiUrl)
Security warning: Values exposed to browser code are not secret. Never put passwords, private API keys or confidential credentials in VITE_ variables. Keep private credentials on a secure server.

Environment variables also do not replace server-side validation or access controls. Validate and authorize requests where the data and operations are handled.

19. Vite and TypeScript

Vite supports TypeScript-based projects and provides templates for popular framework combinations.

For example, create a React and TypeScript project with:

npm create vite@latest my-app -- --template react-ts

Then install its dependencies and run the development server:

cd my-app
npm install
npm run dev

Vite handles the development and build workflow, but a successful Vite build does not by itself guarantee that all TypeScript type errors have been checked. Configure and run the appropriate type-checking command for your project.

20. Browser Support

Vite targets modern browsers by default, with production output affected by the build target you configure. Projects that need support for older browsers may require additional tooling.

Check the official documentation and your users' browser requirements before choosing a target or adding a legacy-browser plugin.

21. Production Builds and the dist Folder

When you are ready to publish your website, run:

npm run build

A typical Vite project writes the generated output to a folder named dist.

dist/
├── index.html
└── assets/
    ├── index-[hash].js
    └── index-[hash].css

This is an illustrative example. Your actual output depends on the project, assets, framework and build configuration.

Important: When deploying a standard static Vite site, publish the contents of the generated dist directory, not the source project folder. Follow your hosting provider's instructions if it builds the project automatically.

22. Deploying a Vite Website

Vite production output can be hosted on many static hosting platforms or served by a web server.

A typical deployment process is:

  1. Finish and test the application locally.
  2. Run npm run build.
  3. Check that the production files were created successfully.
  4. Upload or deploy the output using your hosting provider's process.
  5. Configure the correct base path if the site is hosted in a subdirectory.
  6. Open the public website and test its pages, links, styles and assets.

For applications that use client-side routing, you may also need a hosting rewrite rule so that valid application routes load the correct entry point.

23. Vite vs Webpack

FeatureViteWebpack
Development workflowDesigned for a fast modern development loop.Highly configurable, with behavior depending on setup.
ConfigurationOften straightforward for common projects.Can require more configuration for some projects.
PluginsSupports a plugin ecosystem.Has a mature and extensive plugin ecosystem.
Framework supportWorks with React, Vue, Svelte and other tools.Can support many frontend ecosystems.
Production buildBuilt into the Vite workflow.Provided by Webpack and its configuration.

Neither tool is automatically the best choice for every project. Consider your existing codebase, required plugins, team experience and deployment needs.

24. Advantages of Vite

  • Fast startup in many development projects.
  • Quick feedback through HMR.
  • Simple project scaffolding.
  • Support for popular frontend frameworks.
  • Modern JavaScript module workflow.
  • CSS and asset processing.
  • Built-in production build command.
  • Configuration and plugin options.
  • Useful for prototypes, websites and larger frontend applications.

25. Limitations and Things to Understand

  • Vite does not replace the need to learn HTML, CSS and JavaScript.
  • Normal local development requires Node.js and a supported package manager.
  • Some applications need extra plugins or custom configuration.
  • Large projects may need careful performance and build configuration.
  • Client-side code cannot safely keep confidential credentials secret.
  • Deployment may require routing, base-path or server configuration.
  • Vite does not automatically provide a backend, database or authentication system.

26. Common Beginner Mistakes

Forgetting to install dependencies

After creating or downloading a project, run:

npm install

Using the wrong command

Use the development command while working:

npm run dev

Use the build command to prepare production files:

npm run build

Confusing development with production

The development server is intended for local development. Do not assume that running it is the same as deploying an optimized production build.

Exposing private keys

Do not include private credentials in frontend source files or client-exposed environment variables.

Ignoring asset paths

Incorrect base paths can cause assets to fail when the site is published under a subdirectory. Test the deployed website rather than relying only on local results.

Assuming Vite checks every TypeScript error

Vite handles TypeScript transformations, but you should configure and run type checking separately when required by your project.

27. A Simple Vite Workflow

1. Create a project
        |
        v
2. Install dependencies
        |
        v
3. Start the development server
        |
        v
4. Write and test your code
        |
        v
5. Run the production build
        |
        v
6. Preview the production output
        |
        v
7. Deploy and test the website

Following this workflow helps separate development, testing, building and publishing.

28. Beginner Project Ideas

Practice your Vite skills by building small projects such as:

  • Personal portfolio website.
  • To-do list application.
  • Calculator.
  • Notes application.
  • Weather dashboard using a public API.
  • Expense tracker.
  • Movie search interface.
  • Product catalog.
  • Blog interface.
  • Admin dashboard.

Start with a small application, learn how its files work, and then gradually add more features.

29. Vite Learning Roadmap

  1. Learn HTML structure and semantic elements.
  2. Learn CSS and responsive layouts.
  3. Understand JavaScript fundamentals.
  4. Learn npm and package.json.
  5. Understand JavaScript modules.
  6. Create a Vite project.
  7. Learn the development server and HMR.
  8. Understand project structure and configuration.
  9. Learn how plugins and dependencies work.
  10. Practice production builds.
  11. Deploy a small website.
  12. Explore React, Vue, Svelte or TypeScript if needed.

30. Vite Cheat Sheet

# Create a project
npm create vite@latest

# Enter the project folder
cd my-first-app

# Install dependencies
npm install

# Start development
npm run dev

# Build for production
npm run build

# Preview production output
npm run preview

31. Frequently Asked Questions

Is Vite a programming language?

No. Vite is a frontend build tool and development server.

Is Vite a framework?

No. It can be used with frameworks such as React, Vue and Svelte, but it is not itself a frontend framework.

Can I use Vite without React?

Yes. You can use Vite with plain JavaScript and HTML or with other supported frameworks.

Does Vite work with Vue?

Yes. Vite supports Vue project templates and the associated development workflow.

Does Vite work with Svelte?

Yes. Vite can be used to develop Svelte applications.

Does Vite support TypeScript?

Yes. Vite supports TypeScript-based projects. Configure type checking separately if your workflow requires it.

Does Vite create mobile apps?

Vite is primarily a web development tool. It can be part of a larger workflow for web or hybrid applications, but it is not by itself a native Android or iOS app framework.

Do I need Node.js to use Vite?

Node.js is required for the normal local Vite development workflow. Check the current official documentation for supported versions.

What is the dist folder?

It is the default output directory for a typical Vite production build. It contains the files intended for deployment.

What is HMR?

Hot Module Replacement updates compatible modules during development without requiring a full page reload in many situations.

Can I deploy Vite to static hosting?

Yes. Many Vite applications can be deployed as static sites by building the project and publishing the output directory, provided the application does not require a server-side runtime for its functionality.

Is Vite better than Webpack?

It depends on the project. Vite is designed for a fast modern development experience, while Webpack offers extensive configuration and a mature ecosystem. Evaluate your project's needs before choosing.

Conclusion

Vite is a useful tool for modern frontend development. It provides a development server, Hot Module Replacement, dependency handling, plugins and a production build workflow.

Beginners can start with plain JavaScript and later explore React, Vue, Svelte or TypeScript. Understanding npm, JavaScript modules and the difference between development and production will make Vite easier to learn.

Your next step: Create a small Vite project, run npm run dev, make a few changes, and then run npm run build. Inspect the generated dist folder to understand how your project is prepared for deployment.

↑ Back to top