Skip to content
Latchkey

Astro env var undefined in browser (missing PUBLIC_ prefix) in CI

Astro exposes environment variables through import.meta.env, but only variables prefixed PUBLIC_ reach client-side code; everything else stays server-only for safety. Reading a non-PUBLIC var (or process.env) in the browser yields undefined, which frequently breaks a build check or an end-to-end test in CI.

What this error means

A client-side value is undefined at runtime, or a build/test fails because import.meta.env.API_URL (no PUBLIC_ prefix) is empty in the browser.

astro
TypeError: Cannot read properties of undefined (reading 'toString')
  // import.meta.env.API_URL is undefined in client code (no PUBLIC_ prefix)

Common causes

A non-PUBLIC var read in the client

Client code reads import.meta.env.SOMETHING without the PUBLIC_ prefix, so Astro does not expose it to the browser and it is undefined.

Using process.env in the browser

Client code reads process.env.X; process does not exist in the browser under Astro, so the value is undefined.

How to fix it

Prefix client-safe vars with PUBLIC_

  1. Rename variables the client needs to PUBLIC_....
  2. Read them with import.meta.env.PUBLIC_... in client code.
  3. Set the variable in the CI environment so the build embeds it.
src/components/App.tsx
// client code
const api = import.meta.env.PUBLIC_API_URL;

Keep secrets server-only

Read non-public vars only in server code (.astro frontmatter, endpoints, SSR), never in the client, and never via process.env in the browser.

.github/workflows/ci.yml
env:
  PUBLIC_API_URL: https://api.example.com

How to prevent it

  • Prefix any browser-exposed variable with PUBLIC_.
  • Never read process.env in client code under Astro.
  • Set PUBLIC_ variables in the CI environment so builds embed them.

Frequently asked questions

What causes "import.meta.env undefined in client"?
Client code reads import.meta.env.SOMETHING without the PUBLIC_ prefix, so Astro does not expose it to the browser and it is undefined.
How do I fix import.meta.env undefined in client?
Prefix client-safe vars with PUBLIC_

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card