#Adding env vars to astro.config.mjs
6 messages · Page 1 of 1 (latest)
To add environment variables to the astro.config.mjs file, you can use Vite's loadEnv helper to manually load .env files. Here's an example of how to do this:
import { loadEnv } from "vite";
const { YOUR_ENV_VARIABLE } = loadEnv(import.meta.env.MODE, process.cwd(), "");
Replace YOUR_ENV_VARIABLE with the name of the environment variable you want to use.
Keep in mind that Astro doesn't load .env files inside configuration files by default, so you need to use this method to access them in astro.config.mjs.
Source: Configuring Astro - Environment Variables (https://docs.astro.build/en/guides/configuring-astro#environment-variables)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@graceful escarp kapa.ai is still learning and improving, please let us how it did by reacting below
@slim jacinth do I need to install vite to import it in "astro.config.mjs"
Let me check the knowledge base for you. One moment! :books:
No, you don't need to install Vite separately to import it in astro.config.mjs. Astro already uses Vite internally, so you can directly import the loadEnv helper from Vite without any additional installation.
Here's the example code again for reference:
import { loadEnv } from "vite";
const { YOUR_ENV_VARIABLE } = loadEnv(import.meta.env.MODE, process.cwd(), "");
Replace YOUR_ENV_VARIABLE with the name of the environment variable you want to use.