Deploying to Vercel with Vite JS

beginner
10 min

Deploying to Vercel with Vite JS

Welcome to our comprehensive guide on deploying your Vite JS projects to Vercel! By the end of this tutorial, you'll be able to deploy your own Vite projects with ease. Let's dive in!

šŸŽÆ Introduction

Vite JS is a modern front-end build tool that offers faster development, type-checking, and optimized production builds. Vercel is a platform that makes it easy to deploy, collaborate, and host your projects. This guide will show you how to deploy your Vite projects to Vercel.

šŸ’” Prerequisites

Before we start, make sure you have the following:

  1. Node.js installed (Installation Guide)
  2. Vite installed globally (npm install -g vite)
  3. A project created with Vite (vite create my-project)

šŸ“ Setting up your project for Vercel

To prepare your project for Vercel deployment, follow these steps:

  1. Install vercel CLI: npm install vercel -D
  2. Create a vercel.json file in the project root:
json
{ "version": 2, "builds": [{ "src": ".", "use": "@vercel/vite" }], "public": "dist" }

šŸ’” Pro Tip: The vercel.json file tells Vercel to use Vite for building your project.

šŸ“ Building and deploying to Vercel

Now that your project is set up for Vercel, let's build and deploy it:

  1. Build your project: vite build
  2. Log in to Vercel (vercel login and follow the prompts)
  3. Deploy your project: vercel

Your project is now live on Vercel! You can find the link in your terminal or in the Vercel dashboard.

šŸ“ Vercel Links and Environment Variables

By default, Vercel generates a link for your project. You can also set environment variables to customize your deployment.

  1. Create a vercel.json file with the following content:
json
{ "version": 2, "builds": [{ "src": ".", "use": "@vercel/vite", "env": { "MY_VAR": "my-value" } }], "public": "dist" }

šŸ’” Pro Tip: Replace "my-value" with your desired value for MY_VAR.

šŸ’” Real-world Example

Let's build and deploy a simple Vite project with a custom Vercel link and environment variable:

  1. Create a new Vite project: vite create my-app
  2. Navigate to your project: cd my-app
  3. Install Vercel CLI: npm install vercel -D
  4. Create a vercel.json file:
json
{ "version": 2, "builds": [{ "src": ".", "use": "@vercel/vite", "env": { "API_KEY": "secret-key" } }], "public": "dist" }
  1. Create a simple index.html file:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>My Vite App</title> </head> <body> <h1>Welcome to my Vite app!</h1> <p>My API key is: <strong id="api-key"></strong></p> <script> document.querySelector('#api-key').innerHTML = process.env.API_KEY; </script> </body> </html>
  1. Build and deploy your project:

    vite build vercel

Now, you should see a live deployment with your API key displayed on the page.

šŸ“ Cleaning Up

Once you're done with this tutorial, you can remove the vercel CLI and vercel.json file from your project:

sh
npm uninstall vercel rm vercel.json

šŸ“ Quiz

Quick Quiz
Question 1 of 1

What command do you run to build your Vite project for deployment on Vercel?

That's it for this tutorial! We hope you enjoyed learning about deploying Vite projects to Vercel. Happy coding! šŸŽ‰