Project Settings and Environment Variables

Configure Vercel build settings and environment variables for different deployment environments.

🎯 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:

OptionPurposeVite default
Framework PresetFramework configuration presetVite
Build CommandCommand used to build the projectnpm run build
Output DirectoryDirectory containing the build outputdist
Install CommandCommand used to install dependenciesnpm install
Dashboard paths and option names may change. Paths mentioned here, such as Settings → General, are for reference. If you cannot find an option, browse the project Settings menu or search for its name.

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.

Most projects can keep the defaults. You generally need changes only for a custom build process or code located in a repository subdirectory (Root Directory).

🧱 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_:

src/App.vue
<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.

After adding or updating environment variables, redeploy for the changes to take effect. Vite embeds their values in the output code at build time.

🌍 Three Deployment Environments

When adding a variable, you will see three environment options:

EnvironmentWhen it applies
ProductionProduction deployments (main branch, vercel --prod)
PreviewPreview deployments (other branches, pull requests, vercel)
DevelopmentLocal 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

  1. Add VITE_APP_AUTHOR in the Vercel dashboard with your name as its Production value.
  2. Read it with import.meta.env.VITE_APP_AUTHOR in App.vue and display it on the page.
  3. Redeploy and confirm that the live page shows it correctly.
src/App.vue
<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.