Skip to content
>_VibeUnstuck

ReferenceError: process is not defined

Last verified:

The fix

process is not defined in Vite, including Lovable and Bolt: browser code read process.env. Use import.meta.env.VITE_NAME and restart the dev server.

  1. 1

    Read import.meta.env, with the VITE_ prefix

    Replace process.env.NEXT_PUBLIC_... or process.env.SOMETHING in client code with import.meta.env.VITE_SOMETHING. Restart the dev server after any .env change. Vite loads env files at startup, and the env guide says to restart after you edit them.

    const url = import.meta.env.VITE_SUPABASE_URL;
    if (!url) {
      throw new Error("VITE_SUPABASE_URL is missing");
    }
  2. 2

    Keep secrets off the VITE_ prefix

    A VITE_ value is public. The Supabase URL and the publishable key are public. A service role key, a Stripe secret, or an OpenAI key is not. Call those from a server you control. Do not rename a secret to VITE_ so the reference error goes away.

  3. 3

    Do not define process.env as the whole environment

    Do not add define: { 'process.env': process.env } to silence the error. Vite stringifies non-string define values, which drops every variable from the environment into the bundle. If one public value has to be injected, the config docs show a single key: 'import.meta.env.ENV_VARIABLE': JSON.stringify(process.env.ENV_VARIABLE). Do not set envPrefix to ''.

  4. 4

    Check the built file if this only fails in production

    A dev server can hide a bad env read that the production bundle still contains. Search the built JavaScript for process.env and for the secret's prefix. Fix the source, restart, and build again. The previous bundle keeps the old inlined value.

Why it happens

Vite bundles client code for the browser. The browser has no Node process global, so process.env.SOMETHING throws ReferenceError: process is not defined as soon as that line runs. Vite's env guide says client code reads import.meta.env, and that only variables prefixed with VITE_ are exposed. An unprefixed name is undefined there on purpose. The values are strings, and they are written into the bundle at build time. The same guide says VITE_ variables must not contain secrets such as API keys.

process.env in vite.config is the Node process that is running Vite, which is a different place. A define entry of 'process.env': process.env is a common patch for this error. Vite's config docs say a non-string define value is passed through JSON.stringify, so that one line can embed the whole environment object in the client bundle. Setting envPrefix to an empty string is also rejected, because it would expose every variable.

The next error you'll probably hit

Once this is fixed, the usual follow-up is supabaseUrl is required. supabaseUrl is required: createClient got an empty URL or key, missing when the bundle was built. Set the Supabase URL and anon key, then rebuild.

Prompt to paste back into your AI tool

Paste this above the error.

A Vite app throws ReferenceError: process is not defined. The line uses process.env in browser code.

Do not set define to { 'process.env': process.env }. That can stringify every env var into the client bundle. Do not set envPrefix to an empty string. Do not put a secret in a VITE_ or NEXT_PUBLIC_ variable. Do not disable RLS. Do not swallow this in an empty catch.

1. Find the process.env read in client code and switch it to import.meta.env.VITE_NAME.
2. If the value is a secret, move the call to a server and remove it from the client. Say so instead of adding the VITE_ prefix.
3. Tell me to restart the dev server after the .env change.
4. Touch only that read and the env name.

Common questions

+I copied a Next.js example into a Vite app. Why did process.env break?

Next.js replaces process.env.NEXT_PUBLIC_* in its client bundle. Vite does not provide a process global in the browser. The Vite name is import.meta.env.VITE_*.

+import.meta.env.VITE_NAME is undefined after I added it to .env. Why?

The dev server was not restarted, the name does not start with VITE_, or the variable was added after the production bundle was built. Restart, then build again for production.

Related errors