Vite Environment Variables and Modes
🎯 Introduction
By the end of this article, you will be able to manage environment-specific settings, such as API URLs and application titles, with .env files and read them through import.meta.env. This is a common requirement in real projects.
🧱 Why Use Environment Variables?
The same project often needs different settings in different environments:
- Development uses a local API at
http://localhost:3000. - Testing uses a test server at
https://api-test.example.com. - Production uses the live API at
https://api.example.com.
Hardcoding these URLs means editing the code manually whenever you switch environments, which is error-prone. Environment variables solve this problem.
✨ .env Files
Vite automatically reads .env files in the project root and loads the appropriate files for the current mode:
| File | When it is loaded |
|---|---|
.env | In every mode |
.env.local | In every mode; kept out of Git |
.env.development | Development mode (vite / vite dev) |
.env.production | Production mode (vite build) |
.env.[mode] | A custom mode |
.env.local commonly holds local secrets. Remember to exclude it in .gitignore.🛠 Define Environment Variables
Only variables prefixed with VITE_ are exposed to client code. For example:
VITE_API_URL=http://localhost:3000
VITE_API_PREFIX=/api
Variables without the VITE_ prefix are unavailable to frontend code and are intended for Vite configuration or scripts:
PRIVATE_KEY=abc123
VITE_ prefix. They will be bundled into frontend code that anyone can inspect.🛠 Read Variables in Code
Vite provides environment variables through import.meta.env:
const baseURL = import.meta.env.VITE_API_URL;
console.log(baseURL); // http://localhost:3000
You can also use them directly in a template:
<template>
<p>API URL: {{ import.meta.env.VITE_API_URL }}</p>
</template>
import.meta.env is an object injected by Vite. It includes built-in variables such as MODE, BASE_URL, DEV, and PROD.⚙️ Custom Modes
Specify a mode when running a command:
vite build --mode staging
Vite loads .env.staging and sets import.meta.env.MODE to staging.
You can also access the mode in vite.config.js:
import { defineConfig } from 'vite';
export default defineConfig(({ mode }) => {
console.log('Current mode:', mode);
return {
base: mode === 'production' ? '/app/' : '/',
};
});
defineConfig accepts a function whose argument includes mode and command, allowing you to return different settings for each environment.❓ Questions and Answers
Q1: What are variables without the VITE_ prefix used for?
They can be read in vite.config.js, Node scripts, or backend services, but are not injected into browser code.
Q2: Are .env.local and .env.development loaded together?
Yes. .env.local has higher priority than .env: when a variable exists in both, the value in .env.local overrides the value in .env.
Q3: Where should I define production environment variables?
Define them in .env.production, which is loaded automatically when you run vite build.
🧪 Exercise
Add environment variables to your project:
- Create
.env.developmentand.env.production. - Set a different
VITE_API_URLin each file. - Read and display the API URL in
src/App.vue.
<template>
<p>Current API URL: {{ apiUrl }}</p>
</template>
<script setup>
// Read VITE_API_URL here
const apiUrl = '';
</script>
🎉 You have learned how to use Vite environment variables! Next, we will cover dependency pre-bundling.
