![]() |
VOOZH | about |
Vite is a fast and modern front-end build tool that provides an efficient way to develop web applications. One of its powerful features is the ability to use environment variables, which can help you customize your app's behaviour based on different environments.
This guide will walk you through how to use Vite environment variables (.env files) within your vite.config.js file, allowing you to configure Vite dynamically based on these settings.
Vite supports environment variables that can be defined in .env files. By default, Vite will load environment variables from:
Environment variables that need to be exposed to the Vite application must be prefixed with VITE_. These variables are then accessible in the application via import.meta.env.
Using environment variables in vite.config.js can help you:
Initialize a New Vite Project
npm create vite@latest vite-env-variables-react -- --template react
cd vite-env-variables-react
npm install dotenv
Create a .env File At the root of your project, create a .env file and define your environment variables. Remember to prefix variables with VITE_ to make them available in your Vite configuration and application code.
VITE_API_URL=https://api.example.com
VITE_API_KEY=123456789
VITE_PORT=4000
VITE_USE_LEGACY=true
Edit vite.config.js: In your vite.config.js file, use the dotenv package to load environment variables and configure Vite accordingly.
You can use environment variables directly in your React application code via import.meta.env.
Add scripts for starting the development server and building the project
{
"name": "vite-env-variables-react",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview",
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"@vitejs/plugin-legacy": "^5.4.2",
"dotenv": "^16.4.5",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^9.9.0",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.9",
"globals": "^15.9.0",
"vite": "^5.4.1"
}
}
To start the application run the following command.
npm run devEnvironment variables can be used to enable or disable plugins or configure them conditionally:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import legacy from '@vitejs/plugin-legacy';
// Load environment variables
import dotenv from 'dotenv';
dotenv.config();
const useLegacy = process.env.VITE_USE_LEGACY === 'true';
export default defineConfig({
plugins: [
vue(),
useLegacy && legacy({
targets: ['defaults', 'not IE 11']
}),
].filter(Boolean),
});
If you need to set a dynamic base URL for your assets based on environment:
export default defineConfig({
base: process.env.VITE_ASSET_BASE_URL || '/',
});
Configure the Vite development server with environment-specific settings:
export default defineConfig({
server: {
host: process.env.VITE_SERVER_HOST || 'localhost',
port: parseInt(process.env.VITE_SERVER_PORT, 10) || 3000,
open: process.env.VITE_SERVER_OPEN === 'true',
},
});
VITE_ to avoid conflicts and make it clear which variables are intended for the client-side.