Getting Started

Overview

Vite (French word for “fast”, pronounced /vit/) is a build tool that aims to provide a faster and leaner development experience for modern web projects. It consists of two major parts:

Vite is opinionated and comes with sensible defaults out of the box, but is also highly extensible via its Plugin API and JavaScript API with full typing support.

You can learn more about the rationale behind the project in the Why Vite section.

Browser Support

Scaffolding Your First Vite Project

Compatibility Note

Vite requires Node.js version >=12.0.0.

With NPM:

  1. $ npm init @vitejs/app

With Yarn:

  1. $ yarn create @vitejs/app

Then follow the prompts!

You can also directly specify the project name and the template you want to use via additional command line options. For example, to scaffold a Vite + Vue project, run:

  1. # npm 6.x
  2. npm init @vitejs/app my-vue-app --template vue
  3. # npm 7+, extra double-dash is needed:
  4. npm init @vitejs/app my-vue-app -- --template vue
  5. # yarn
  6. yarn create @vitejs/app my-vue-app --template vue

Supported template presets include:

  • vanilla
  • vue
  • vue-ts
  • react
  • react-ts
  • preact
  • preact-ts
  • lit-element
  • lit-element-ts
  • svelte
  • svelte-ts

See @vitejs/create-app for more details on each template.

Community Templates

@vitejs/create-app is a tool to quickly start a project from a basic template for popular frameworks. Check out Awesome Vite for community maintained templates that include other tools or target different frameworks. You can use a tool like degit to scaffold your project with one of the templates.

  1. npx degit user/project my-project
  2. cd my-project
  3. npm install
  4. npm run dev

If the project uses main as the default branch, suffix the project repo with #main

  1. npx degit user/project#main my-project

index.html and Project Root

One thing you may have noticed is that in a Vite project, index.html is front-and-central instead of being tucked away inside public. This is intentional: during development Vite is a server, and index.html is the entry point to your application.

Vite treats index.html as source code and part of the module graph. It resolves <script type="module" src="..."> that references your JavaScript source code. Even inline <script type="module"> and CSS referenced via <link href> also enjoy Vite-specific features. In addition, URLs inside index.html are automatically rebased so there’s no need for special %PUBLIC_URL% placeholders.

Similar to static http servers, Vite has the concept of a “root directory” which your files are served from. You will see it referenced as <root> throughout the rest of the docs. Absolute URLs in your source code will be resolved using the project root as base, so you can write code as if you are working with a normal static file server (except way more powerful!). Vite is also capable of handling dependencies that resolve to out-of-root file system locations, which makes it usable even in a monorepo-based setup.

Vite also supports multi-page apps with multiple .html entry points.

Specifying Alternative Root

Running vite starts the dev server using the current working directory as root. You can specify an alternative root with vite serve some/sub/dir.

Command Line Interface

In a project where Vite is installed, you can use the vite binary in your npm scripts, or run it directly with npx vite. Here is the default npm scripts in a scaffolded Vite project:

  1. {
  2. "scripts": {
  3. "dev": "vite", // start dev server
  4. "build": "vite build", // build for production
  5. "serve": "vite preview" // locally preview production build
  6. }
  7. }

You can specify additional CLI options like --port or --https. For a full list of CLI options, run npx vite --help in your project.

Using Unreleased Commits

If you can’t wait for a new release to test the latest features, you will need to clone the vite repo to your local machine and then build and link it yourself (Yarn 1.x is required):

  1. git clone https://github.com/vitejs/vite.git
  2. cd vite
  3. yarn
  4. cd packages/vite
  5. yarn build
  6. yarn link

Then go to your vite based project and run yarn link vite. Now restart the development server (yarn dev) to ride on the bleeding edge!

Community

If you have questions or need help, reach out to the community at Discord and GitHub Discussions.