Auth & Data
Use the Neon serverless HTTP driver in a Next.js app that must build without a database: lazy connection, a guarded client, and pooled vs direct URL choices.
1 file
Description
Use the Neon serverless HTTP driver in a Next.js app that must build without a database: lazy connection, a guarded client, and pooled vs direct URL choices.
Standing up the first DB connection in a Next.js app deployed where the build step may not have secrets (Vercel build, a CI without env), or debugging a build that crashes on a missing DATABASE_URL.
A top-level neon(process.env.DATABASE_URL!) throws at import time when the var is absent, which crashes next build. Defer construction until the first query with a lazy proxy so import is always safe:
import { neon } from "@neondatabase/serverless";
import { drizzle, type NeonHttpDatabase } from "drizzle-orm/neon-http";
import * as schema from "./schema";
let cached: NeonHttpDatabase<typeof schema> | null = null;
function getDb() {
if (cached) return cached;
const sql = neon(process.env.DATABASE_URL ?? "postgresql://placeholder@localhost/placeholder");
cached = drizzle(sql, { schema });
return cached;
}
export const db = new Proxy({} as NeonHttpDatabase<typeof schema>, {
get: (_t, prop) => (getDb() as Record<string | symbol, unknown>)[prop as string],
});
The placeholder URL keeps import from throwing; queries lazy-fail at runtime instead, which is the correct place for a missing-secret error.
neon() HTTP driver is one round trip per query, ideal for serverless and edge functions with no persistent connection to leak.neon's transaction helper or the WebSocket Pool driver.process.env.DATABASE_URL at module top level is the single most common cause of a build that passes locally (where .env.local exists) and fails in CI.transaction() batches statements but is not a general interactive transaction. Do not rely on running arbitrary logic between statements inside it.Pool (TCP) driver. Keep the HTTP driver for edge routes.Related
Added 2026-07-01. Back to the Skill Library.

New tutorials, open-source projects, and deep dives on coding agents - delivered weekly.