🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Fix: Astro envPrefix Conflicts With Secret Variables

Fix the Astro envPrefix conflicts error: a secret in env.schema matches a vite.envPrefix prefix. Remove the prefix or rename the secret so it never reaches the client.

#Astro#Errors#Environment Variables#Security

The Astro envPrefix conflicts error, titled “envPrefix conflicts with secret environment variables.”, means a variable you declared as a secret in env.schema starts with a prefix listed in vite.envPrefix. Vite exposes every variable with those prefixes to client-side code, so the secret would end up in your JavaScript bundles. Astro stops the build instead. Remove the prefix from vite.envPrefix, or rename the secret so it doesn’t start with it.

Key Takeaways

  • vite.envPrefix decides which variables Vite inlines into browser code. PUBLIC_ is Astro’s default.
  • A variable with access: 'secret' must never match one of those prefixes.
  • Astro checks this at startup, only when you set vite.envPrefix, and lists every conflicting variable.
  • Secrets belong to context: 'server'; read them from astro:env/server only.

The full message

`envPrefix` conflicts with secret environment variables.

The following environment variables are declared with `access: "secret"` in `env.schema`,
but their names match a prefix in `vite.envPrefix`, which would expose them in client-side bundles:

- APP_STRIPE_SECRET

Either remove the conflicting prefixes from `vite.envPrefix`, or rename these variables
to use a prefix not in `vite.envPrefix`.

What causes it

This config triggers the error:

// astro.config.mjs
import { defineConfig, envField } from 'astro/config';

export default defineConfig({
  vite: {
    envPrefix: ['PUBLIC_', 'APP_'],
  },
  env: {
    schema: {
      APP_STRIPE_SECRET: envField.string({ context: 'server', access: 'secret' }),
      PUBLIC_SITE_NAME: envField.string({ context: 'client', access: 'public' }),
    },
  },
});

Adding APP_ to envPrefix, often to make variables from another framework visible, tells Vite to inline every APP_* variable into client code through import.meta.env. APP_STRIPE_SECRET would go along with them, regardless of the schema saying it’s secret.

Fix 1: rename the secret

Give secrets a name no client prefix matches:

// astro.config.mjs
env: {
  schema: {
    STRIPE_SECRET_KEY: envField.string({ context: 'server', access: 'secret' }),
  },
},

Rename it in .env, in your host’s environment settings, and wherever you read it:

// src/pages/api/checkout.ts
import { STRIPE_SECRET_KEY } from 'astro:env/server';

Fix 2: drop the extra prefix

If you added a prefix only for convenience, remove it and go back to the default:

// astro.config.mjs
export default defineConfig({
  // no vite.envPrefix: Astro uses PUBLIC_
  env: {
    schema: {
      PUBLIC_SITE_NAME: envField.string({ context: 'client', access: 'public' }),
      APP_STRIPE_SECRET: envField.string({ context: 'server', access: 'secret' }),
    },
  },
});

Variables the browser needs should be declared in the schema with context: 'client' and access: 'public', and read from astro:env/client. That makes what’s exposed explicit, rather than depending on a name prefix.

Check nothing already leaked

If a build ever succeeded with the old setup, search the built output for the secret’s value:

# Terminal: replace with a recognizable part of the real value
grep -r "sk_live_" dist/ && echo "Secret found in build output"

If it shows up, rotate the key with the provider; it may have been deployed.

The environment variables guide explains astro:env contexts, the invalid environment variables fix covers missing or mistyped values, and our Astro config docs show the config file in Komal Astro themes. The official astro:env guide lists every field type.

FAQ

What is vite.envPrefix in Astro?

A Vite option listing name prefixes for variables that get inlined into client code through import.meta.env. Astro’s default is PUBLIC_.

Can a secret start with PUBLIC_?

Don’t name one that way. Astro only runs this conflict check when you set vite.envPrefix yourself, so a secret called PUBLIC_... under the default setup isn’t caught by this error. Treat PUBLIC_ as reserved for values that are safe in the browser.

Do I need vite.envPrefix with astro:env?

Usually not. Declare client variables in env.schema with context: 'client' and read them from astro:env/client.