Deploy a Neo4j app on Vercel
To run a Neo4j-backed app on Vercel, point it at a Neo4j 5.6+ database that is reachable over the
internet (Neo4j Aura, Cyphra Cloud or your own server with TLS), set NEO4J_URI,
NEO4J_USERNAME and NEO4J_PASSWORD in the project’s environment variables, apply migrations in
the build command (cyphra migrate deploy && next build), and create one database client per
function instance instead of one per request.
The fastest start is the Next.js starter , whose README has a Deploy with Vercel button that asks for those three variables. This guide explains each piece so you can apply it to your own app. The app side is covered in the Next.js guide.
What we tested: the build command, against a local Neo4j 5.26, including what happens when the database is unreachable or a variable is missing, the client code below on the Node.js runtime, and the HTTP transport (the ORM suite against Neo4j 5.26 and an Edge-style bundle with no Node.js built-ins). Vercel platform behavior (environment scopes, instance reuse) is described from Vercel’s documentation.
Steps
Get a database Vercel can reach
Vercel’s builds and functions connect from Vercel’s infrastructure, not from your machine, so
bolt://127.0.0.1 from cyphra dev will not work. Use a database with a public, encrypted endpoint
and a neo4j+s:// URI:
- Neo4j Aura, Neo4j’s managed service.
- Cyphra Cloud, a dedicated Neo4j Community instance per database.
- Any self-hosted Neo4j 5.6 or later with TLS on the Bolt port.
Cyphra’s typed client needs Neo4j 5.6+, because it compiles relations to COLLECT { } subqueries.
Set the environment variables
In the Vercel project, under Settings → Environment Variables, add:
| Name | Value |
|---|---|
NEO4J_URI | neo4j+s://<your-host> |
NEO4J_USERNAME | usually neo4j |
NEO4J_PASSWORD | the database password |
NEO4J_DATABASE | optional, for a non-default database (CLI only; pass database to createClient in app code) |
Vercel makes these available both during the build (which cyphra migrate deploy needs) and at
runtime. Never prefix them with NEXT_PUBLIC_.
Variables can be scoped to Production, Preview and Development. Give Preview its own database: every preview deployment runs the build, so with the setup below it also runs your migrations against whatever database Preview points to.
@cyphra/cli versions before 0.2.1 read NEO4J_USER, not NEO4J_USERNAME. If the build fails
with “Set NEO4J_URI, NEO4J_USER, and NEO4J_PASSWORD”, upgrade the CLI or also set NEO4J_USER.
Run migrations in the build
{
"scripts": {
"build": "cyphra migrate deploy && next build",
"postinstall": "cyphra generate"
}
}postinstallregenerateslib/cyphra.gen.tsafter Vercel installs dependencies, so the generated client does not need to be committed.cyphra migrate deployapplies the migrations inmigrations/that the database has not seen yet, and does nothing when there are none. It never generates new migrations: create those locally withcyphra migrate devand commit them.- If it fails, the build fails and the previous deployment keeps serving. We checked both failure
modes: an unreachable database (“Failed to connect to server…”) and a missing variable (“Set
NEO4J_URI, NEO4J_USERNAME and NEO4J_PASSWORD in the environment”) each exit with code 1, so
next buildnever runs.
Prefer to migrate outside the build, for example in a CI job before promoting a deployment? Run
npx cyphra migrate deploy there with the same three variables and keep next build alone in the
build command. See Migrations for what a migration contains.
Reuse one client per function instance
import "server-only";
import { createClient } from "./cyphra.gen";
const globalForDb = globalThis as unknown as {
cyphra?: ReturnType<typeof createClient>;
};
export function db() {
globalForDb.cyphra ??= createClient({
uri: process.env.NEO4J_URI!,
user: process.env.NEO4J_USERNAME!,
password: process.env.NEO4J_PASSWORD!,
driverConfig: { maxConnectionPoolSize: 10, connectionAcquisitionTimeout: 10_000 },
});
return globalForDb.cyphra;
}Vercel can serve many invocations from the same function instance. A client created at module
scope (or cached on globalThis, as here and in the Next.js guide) lives as long
as the instance, so later requests reuse its open connections. Creating a client inside each request
handler would open a new connection, with a TLS handshake and authentication, on every request.
Don’t call db().$disconnect() at the end of a request either: that closes the pool the next
request would reuse.
driverConfig is passed to the Neo4j driver. maxConnectionPoolSize caps the connections one
instance can hold, which matters when many instances run at once against a database with a
connection limit; connectionAcquisitionTimeout (milliseconds) bounds how long a query waits for a
free connection. Size both for your database and traffic; the values above are only an example.
The default Node.js runtime connects over Bolt, with the pool above. The Edge runtime has
no TCP sockets, so there the client talks to Neo4j over HTTPS with the
HTTP transport (Neo4j 5.19+ Query API, available on Aura):
pass transport: "http" and keep the same neo4j+s:// NEO4J_URI, which maps to https:// on
the same host. The generated client needs no change: the Edge bundle picks Cyphra’s build without
neo4j-driver.
import { createClient } from "@/lib/cyphra.gen";
export const runtime = "edge";
const db = createClient({
uri: process.env.NEO4J_URI!,
transport: "http",
user: process.env.NEO4J_USERNAME!,
password: process.env.NEO4J_PASSWORD!,
});
export async function GET() {
return Response.json(await db.post.findMany({ take: 20 }));
}Over HTTP every statement is a request and a write transaction adds a commit request, so Edge
routes suit short reads and small writes close to the database’s region. Keep heavy or
multi-statement work on the Node.js runtime, and cyphra migrate deploy in the build (it uses
Bolt). See the limitations.
Deploy and check
Push to your Git provider or run vercel deploy. In the build logs, cyphra migrate deploy
prints Applied: <migration> for each new migration before Next.js starts building. Then open a
page or Route Handler that queries the database.
Troubleshooting
| Symptom in the build or function logs | Likely cause |
|---|---|
Set NEO4J_URI, NEO4J_USERNAME and NEO4J_PASSWORD in the environment | A variable is missing for this environment (Production / Preview), or misspelled |
Failed to connect to server | URI or port wrong, database paused or private, or bolt:// used where TLS is required |
The client is unauthorized due to authentication failure. | Wrong username or password |
Module not found: Can't resolve './cyphra.gen' | cyphra generate did not run: check the postinstall script and the generate path in cyphra.json |
| Data on a page is stale until the next deploy | The page was prerendered at build time: see Read in a Server Component |
Related
- Next.js guide
- Cyphra Cloud: managed Neo4j with
neo4j+s://endpoints, and pricing - CLI:
migrate deploy,migrate statusand environment variables - Neo4j migrations: constraints and indexes as code