@rollin
To load environment variables in a Vite Svelte project for production, you can use the dotenv package.
Here are the steps to load environment variables in Vite Svelte for production:
1
|
npm install dotenv |
1
|
VITE_API_URL=https://example.com/api |
1 2 3 4 5 6 7 8 9 10 11 |
import { defineConfig } from 'vite'; import { svelte } from '@sveltejs/vite-plugin-svelte'; import dotenv from 'dotenv'; dotenv.config(); // Use process.env.VITE_API_URL to access the environment variable in your code export default defineConfig({ plugins: [svelte()] }); |
1
|
npm run build |
This will create a production-ready build of your Svelte project with the environment variables loaded from the .env file.