#Experimental private meta environment variables inlining

4 messages · Page 1 of 1 (latest)

timid elk
wheat kettleBOT
#

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

#

With Astro version 5.13, a new experimental flag called staticImportMetaEnv was introduced. This flag changes how Astro handles environment variables accessed via import.meta.env to align with Vite’s behavior.

Key changes with this update:

  • No more type coercion: Previously, Astro might convert string values from import.meta.env into booleans or numbers (e.g., "true" to true, "1" to 1). With this flag enabled, values are no longer coerced; they remain as strings, just like in Vite.
  • No more process.env fallback: Astro used to replace some import.meta.env variables with references to process.env (especially for non-public variables). With the flag enabled, this replacement no longer happens—import.meta.env values are always inlined.
  • Future default: This behavior will become the default in Astro 6.0, so enabling the flag now helps future-proof your project.
  • Manual updates may be needed: If your code relied on the old coercion or process.env fallback, you may need to update your code. For example, you might need to manually convert string values to booleans or numbers, or switch to using process.env directly for certain variables.

How to enable:

// astro.config.mjs
import { defineConfig } from "astro/config"
export default defineConfig({
  experimental: {
    staticImportMetaEnv: true,
  }
})

Example update:

If you previously relied on coercion:

const enabled = import.meta.env.ENABLED === "true";

If you previously relied on process.env:

const password = process.env.DB_PASSWORD;