Project Settings and Environment Variables
🎯 Introduction
In this article, you will learn to view and adjust your Vercel project's build settings, including its build command and output directory, and configure environment variables to use different values in preview and production environments.
🧱 Project Build Settings
When you imported your project, Vercel detected its framework and filled in the build configuration. You can change it at any time. Open your project → Settings → General → Build & Development Settings to find four key options:
| Option | Purpose | Vite default |
|---|---|---|
| Framework Preset | Framework configuration preset | Vite |
| Build Command | Command used to build the project | npm run build |
| Output Directory | Directory containing the build output | dist |
| Install Command | Command used to install dependencies | npm install |
When would you change these? If you create a custom build script such as npm run build:prod, set it as the Build Command. After saving, it takes effect on the next deployment.
🧱 Why Use Environment Variables?
Suppose your project calls a backend API with different local and production URLs:
- Local:
http://localhost:3000/api - Production:
https://api.example.com
Hardcoding the URL means editing and committing code whenever you switch environments. Environment variables extract these changing values so the deployment platform can inject them at build time.
Vite reads environment variables through import.meta.env.VARIABLE_NAME, as covered in the Stage4 Vite course. Names must start with VITE_:
<script setup>
const apiUrl = import.meta.env.VITE_API_URL;
console.log('Current API URL:', apiUrl);
</script>
🛠 Configure Environment Variables in Vercel
Open your project → Settings → Environment Variables and add a variable:
- Name:
VITE_API_URL - Value:
https://api.example.com - Environments: Select
Production.
Save it, then return to Deployments and click Redeploy for the latest deployment (or push another commit). The variable will be injected into the new build.
🌍 Three Deployment Environments
When adding a variable, you will see three environment options:
| Environment | When it applies |
|---|---|
| Production | Production deployments (main branch, vercel --prod) |
| Preview | Preview deployments (other branches, pull requests, vercel) |
| Development | Local development (vercel dev, covered in the final article) |
You can assign different values to the same variable in each environment. For example, set VITE_API_URL to your live API in Production and your test API in Preview. Preview deployments then connect to the test server independently of production.
❓ Knowledge Check
Q1: Is it safe to put a secret key in a VITE_ variable?
No! Variables prefixed with VITE_ are bundled into frontend code and visible to anyone in the browser. Never put backend secrets there. Use a serverless function instead, as covered in article 7.
Q2: Why did changing an environment variable have no effect on the live site?
Two common causes are forgetting to redeploy and omitting the VITE_ prefix, which prevents Vite from exposing the variable to frontend code.
Q3: How do local .env files relate to Vercel environment variables?
They are maintained separately. Use .env for local development and dashboard variables for Vercel deployments. Keep both sets of values up to date.
Q4: What if a variable is not configured for Preview?
It will be undefined in that environment. If preview deployments need it, select Preview and assign a value too.
🧪 Practice
- Add
VITE_APP_AUTHORin the Vercel dashboard with your name as its Production value. - Read it with
import.meta.env.VITE_APP_AUTHORinApp.vueand display it on the page. - Redeploy and confirm that the live page shows it correctly.
<script setup>
// Write your code here
</script>
🎉 You now understand project settings and environment variables! Next, we will cover preview deployments and rollbacks to make releases easier to manage.
