AI with AIlabs
← All guides
3 min read

My production build failed because a database client ran too early

A pre-deploy gate caught a hidden dependency on a database URL, and the fix was one quiet file.


Build Logdeploy-gateNext.jsNeonVercelClaude Code

Learn · Nov 14, 2026

I ran a gate on my own site before promoting it. It failed. Not on something clever. On a database URL.

The site is AI with AI Labs, the one you're reading right now. I've been building the Lead Magnet Maker into it, and I wanted a hard check before the next production deploy. So I ran deploy-gate, a skill that does a production-shaped build and tells me what breaks.

Here's the thing. The app ran fine on my machine. It ran fine on Vercel. The gate only failed because it built the site without the database variable set. That's a situation I'd never tested, and it's exactly the kind that bites you later.

Run the build the way production would

Most of my testing happens with everything set. Every variable present, every service reachable. That tells me the app works when nothing is wrong. It tells me nothing about a build that starts with a missing piece. So the gate builds the app twice. Once with a required environment variable, once without it. If the second build dies, I've found a hidden dependency. It died. The build needed DATABASE_URL, and nothing in the build was supposed to need it.

Find the line that runs too early

The cause was one file, lib/db/index.ts. It called neon() at the top level. That means the database client got created the moment the file was loaded. I'd never thought about that. I'd thought about when queries run. Not when the file loads. But Next imports every route's modules while it builds. So a file that looks like it only matters at request time gets loaded during the build, and its top-level code runs with it. No query was ever sent. The build still needed the URL, just to import the file.

Create the client when something asks for it

The fix was small. I stopped creating the client at import time. It's now created only when something actually needs it. That's the whole change. Same client, same queries, same behavior at request time. The only difference is that loading the file no longer does anything. It's a good habit for any module that talks to a service. Keep the top level of the file quiet. Let the first real call do the work.

Add a verify script so the check repeats

I added a verify script in the same commit. Finding a problem once is useful. Finding it again next month, without remembering how I found it, is the real goal. The script means I don't rely on memory. I run one command and the check runs again. If I add another top-level client later, the same failure shows up before it reaches production.

Ship the first slice of the Lead Magnet Maker

With the gate green, the next deploys were the maker itself. Step one was the config schema. I used zod, and a discriminated union over the four templates: scorecard, calculator, checklist, and generator. Each template has its own shape, and the schema knows which one it's looking at. I also ported the prototype's factory function into typed defaults. The prototype was the thing I'd been playing with. Now the real code has the same starting points, but with types that catch mistakes. I like building the shape first. Every screen after this reads from one validated config, so there's less room for the UI and the data to disagree.

Add design options and AI edit in preview

The last deploy of the day added the part people will actually see. There are four layouts: stacked, side by side, single column, and results first. There are six project styles, an accent color, a corner radius, and a system type family. I also added an AI edit-in-preview feature. You change the design and see it in the preview, with AI helping with the edit. None of that went out blind. The gate had already cleared the build path, so I could ship the feature work without wondering if the foundation was shaky.

What broke: The production build failed whenever DATABASE_URL wasn't set. The cause was lib/db/index.ts, which created the Neon client at the top of the file. Next imports every route's modules during a build, so that line ran even though no request and no query existed. The build needed a secret just to load a file.

The fix: I moved the client creation out of import time. It's now created only when something needs it, so loading the file does nothing. I also added a verify script so the same check runs again on demand. The next production deploy built cleanly, and the two after it did too. Next I want the gate to run on every push, so I stop being the only thing standing between a quiet top-level line and a failed deploy.

Find code that runs at import time
You're reviewing my Next.js app for code that runs when a file loads, not when a request arrives.

Search the codebase for top-level code in server modules that:
1. Creates a client for a database, API, or payment service.
2. Reads an environment variable and throws if it's missing.
3. Opens a connection or makes a network call.

For each one, tell me:
- the file and line
- what would happen during `next build` if that environment variable were absent
- the smallest change that delays the work until the first real call

Then write a script I can run to build the app with each required variable unset, one at a time, and report which builds fail. Don't change any files until I've read your list.

Get new guides and builds in your inbox

No noise. New artifacts, skills, and lessons when they drop — and early access when agents launch.

next_steps.exe

Want a full structured path instead of one-off guides?

That's Learn AI with AI — a sequenced course that takes you from zero to building real things, with community and accountability built in.

Check out Learn AI with AI →