#How to use .env variable in config file

2 messages · Page 1 of 1 (latest)

warm rapids
#

It seems that .env variables are not usable inside a config file. How can I use an .env variable inside my config file (for the base property) currently? Any workarounds? Seems there is also no CLI flag to set the base prop manually via CLI.

thick glade
#

Officially according to the docs, the use of .env variables in the astro.config.mjs file is not supported: https://docs.astro.build/en/guides/configuring-astro/#environment-variables

This might be a workaround, but I have only tested it in development and not in production. I'm also not sure if there are any security concerns with this workaround (like potentially exposing private keys to the client) so use at your own risk.

astro.config.mjs:

import { defineConfig } from 'astro/config';
import * as dotenv from 'dotenv';
dotenv.config();

// You can now access process.env.YOUR_ENV_VAR
export default defineConfig({});

Hat tip: #storyblok message

Astro Documentation

Build faster websites with less client-side Javascript.