The Agent Ledger

← All fixes

Fix: "supabaseUrl is required" when your AI-built Next.js app fails to build

By The Agent Ledger. Drafted with AI assistance and reviewed by the publisher. Tested 2026-10-18.

Status: Reproduced and fixed on a synthetic app (build only). We reproduced the error and verified the fix on 2026-10-18 with a small invented Next.js app on one Linux machine (Node 20.19.2, next 16.4.2, @supabase/supabase-js 2.109.0). We verified that `next build` fails before the fix and passes after, and that the fixed route returns a clear error when keys are missing. We did NOT test against a real Supabase project, a real insert, Vercel/Netlify, Lovable, Bolt or Cursor-generated code, or any customer system. No private data was used.

Short answer: Error: supabaseUrl is required. during next build usually means a file creates the Supabase client at the top of the file, and your Supabase variables are not set while the build runs. The smallest fix is to create the client inside a function, so it is only built when a request arrives. Verified on a synthetic app only. You may also need to add the variables in your host's settings (see "Not verified").

Prerequisites

The exact error

From our reproduction (npx next build):

  Collecting page data using 5 workers ...
Error: Failed to collect configuration for /api/waitlist
    at ignore-listed frames {
  [cause]: Error: supabaseUrl is required.
      at module evaluation (app/api/waitlist/route.js:3:7)
    1 | import { createClient } from "@supabase/supabase-js";
    2 |
  > 3 | const supabase = createClient(
      |       ^
...
> Build error occurred
Error: Failed to collect page data for /api/waitlist

Exit code: 1. Other people report the same text in public threads, for example vercel/next.js#69095 (a Vercel build on Next 14.2.3) and supabase/supabase#19377. We found these through web search; we did not measure how common it is.

Versions we used

Item Version
Node.js 20.19.2
npm 11.21.0
next 16.4.2 (Turbopack build)
react 19.3.1
@supabase/supabase-js 2.109.0
OS Linux, single machine

Cause

Next.js loads your API route files during next build to collect page data. If a route file calls createClient(process.env.NEXT_PUBLIC_SUPABASE_URL, ...) at the top level, that call runs during the build. If the variable is missing in that environment (not in .env.local, not set on the host, or misspelled), createClient throws, and the whole build fails. AI tools often generate this top-level pattern.

Smallest working fix

Move createClient into a function and call it inside the request handler. Nothing else changes.

Before (fails)

import { createClient } from "@supabase/supabase-js";

const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
);

export async function POST(req) {
  const { email } = await req.json();
  const { error } = await supabase.from("waitlist").insert({ email });
  return Response.json({ ok: !error });
}

After (builds)

import { createClient } from "@supabase/supabase-js";

function getSupabase() {
  const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
  const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
  if (!url || !key) {
    throw new Error("Missing NEXT_PUBLIC_SUPABASE_URL or NEXT_PUBLIC_SUPABASE_ANON_KEY");
  }
  return createClient(url, key);
}

export async function POST(req) {
  try {
    const { email } = await req.json();
    const { error } = await getSupabase().from("waitlist").insert({ email });
    return Response.json({ ok: !error });
  } catch (e) {
    return Response.json({ ok: false, error: e.message }, { status: 500 });
  }
}

Copyable prompt for your AI coding tool

My Next.js build fails with "Error: supabaseUrl is required." Find every file (API routes, server actions, lib files) that calls createClient from @supabase/supabase-js at the top level of the module. Change each one so createClient is called inside a function (getSupabase) that reads process.env at request time, and throws a clear error naming the missing variable. Do not change variable names, do not add or print any key values, and do not touch anything else. Show me the diff.

Review the diff yourself. We did not test this prompt in any AI tool.

How we verified

All commands were run on 2026-10-18 in fix-01/before and fix-01/after, with no .env file and no Supabase variables set.

  1. Before: npx next build → failed with the error above, exit code 1. (Log: before-build.log)
  2. After: npx next build → exit code 0. The output listed ƒ /api/waitlist as a dynamic route. (Log: after-build.log)
  3. After, runtime with missing keys: we ran npx next start and sent POST /api/waitlist with a fake email. Response: HTTP/1.1 500 with body {"ok":false,"error":"Missing NEXT_PUBLIC_SUPABASE_URL or NEXT_PUBLIC_SUPABASE_ANON_KEY"}. (Log: after-runtime.log)

Files: see the downloads below.

What this fix does and does not do

Not verified

Troubleshooting

Security notes

Downloads

Synthetic example files from our test. No real keys or data.

Get The Agent Ledger every Tuesday

Free. One practical AI automation per week, explained in plain language, with a clear monthly cost estimate and what you still do yourself.

Free to subscribe. One email a week, on Tuesdays. Unsubscribe any time. We use your email only to send the newsletter. Privacy