#How do i use enviroment variables in the astro config

3 messages · Page 1 of 1 (latest)

spring briarBOT
#

Hi @wary gale. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro evaluates configuration files before it loads your other files. Therefore, you can't use import.meta.env to access environment variables that were set in .env files. However, you can use process.env in a configuration file to access other environment variables, like those set by the CLI.

If you need to manually load .env files, you can use Vite’s loadEnv helper. Here's an example of how to use it:

import { loadEnv } from "vite";
const { SECRET_PASSWORD } = loadEnv(process.env.NODE_ENV, process.cwd(), "");

Please note that if you are using pnpm, you will need to install vite to use the loadEnv helper. You can do this by running the following command:

pnpm add vite --save-dev

For more information, you can refer to the Astro documentation on environment variables (https://docs.astro.build/en/guides/configuring-astro#environment-variables) and the Vite documentation on using environment variables in config (https://main.vitejs.dev/config/#using-environment-variables-in-config).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wary gale kapa.ai is still learning and improving, please let us know how it did by reacting below