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!
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.
Before we start, make sure you have the following:
npm install -g vite)vite create my-project)To prepare your project for Vercel deployment, follow these steps:
vercel CLI: npm install vercel -Dvercel.json file in the project root:{
"version": 2,
"builds": [{ "src": ".", "use": "@vercel/vite" }],
"public": "dist"
}š” Pro Tip: The vercel.json file tells Vercel to use Vite for building your project.
Now that your project is set up for Vercel, let's build and deploy it:
vite buildvercel login and follow the prompts)vercelYour project is now live on Vercel! You can find the link in your terminal or in the Vercel dashboard.
By default, Vercel generates a link for your project. You can also set environment variables to customize your deployment.
vercel.json file with the following content:{
"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.
Let's build and deploy a simple Vite project with a custom Vercel link and environment variable:
vite create my-appcd my-appnpm install vercel -Dvercel.json file:{
"version": 2,
"builds": [{ "src": ".", "use": "@vercel/vite", "env": { "API_KEY": "secret-key" } }],
"public": "dist"
}index.html file:<!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>Build and deploy your project:
vite build
vercel
Now, you should see a live deployment with your API key displayed on the page.
Once you're done with this tutorial, you can remove the vercel CLI and vercel.json file from your project:
npm uninstall vercel
rm vercel.jsonWhat 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! š