Nextjs, Prisma and generate schema #1929

Closed
opened 2026-03-13 09:13:07 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @JohnKesko on GitHub (Sep 15, 2025).

Am I misunderstanding something when generating the schema?
npx @better-auth/cli@latest generate - should generate the schema.

My setup with Nextjs:

/project/prisma/schema.prisma

generator client {
  provider = "prisma-client-js"
  output   = "./generated/prisma" <--- will generate to:  /project/prisma/generated/prisma
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

/project/app/lib/db.ts

  1. Due to hot reloading and possible port exhaustion in Nextjs, we need db.ts:
import { PrismaClient } from "../prisma/generated/prisma/client";

const globalForPrisma = global as unknown as { prisma: PrismaClient };

export const db = globalForPrisma.prisma || new PrismaClient();

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db;
  1. /project/app/lib/auth.ts:
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { db } from "./db";

export const auth = betterAuth({
	database: {
		provider: prismaAdapter(db, {
			provider: "postgresql",
		}),
	},
	emailAndPassword: {
		enabled: true,
	},
});
  1. Generating the schema:
    npx @better-auth/cli generate

Results in:
ERROR [Better Auth]: Failed to initialize database adapter

Doesn't matter if I provide the path to the config:
npx @better-auth/cli generate --config "lib/auth.ts"
ERROR [Better Auth]: Failed to initialize database adapter

Am I missing something obvious?

Originally created by @JohnKesko on GitHub (Sep 15, 2025). Am I misunderstanding something when generating the schema? npx @better-auth/cli@latest generate - should generate the schema. My setup with Nextjs: /project/prisma/schema.prisma ``` generator client { provider = "prisma-client-js" output = "./generated/prisma" <--- will generate to: /project/prisma/generated/prisma } datasource db { provider = "postgresql" url = env("DATABASE_URL") } ``` /project/app/lib/db.ts 1. Due to hot reloading and possible port exhaustion in Nextjs, we need db.ts: ``` import { PrismaClient } from "../prisma/generated/prisma/client"; const globalForPrisma = global as unknown as { prisma: PrismaClient }; export const db = globalForPrisma.prisma || new PrismaClient(); if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db; ``` 2. /project/app/lib/auth.ts: ``` import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { db } from "./db"; export const auth = betterAuth({ database: { provider: prismaAdapter(db, { provider: "postgresql", }), }, emailAndPassword: { enabled: true, }, }); ``` 3. Generating the schema: npx @better-auth/cli generate Results in: ERROR [Better Auth]: Failed to initialize database adapter Doesn't matter if I provide the path to the config: npx @better-auth/cli generate --config "lib/auth.ts" ERROR [Better Auth]: Failed to initialize database adapter Am I missing something obvious?
GiteaMirror added the nextjs label 2026-03-13 09:13:07 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 15, 2025):

Hi @JohnKesko! I'm Dosu and I’m helping the better-auth team.

You’re not missing anything obvious—this error is usually caused by the custom output path in your Prisma client generator. The Better Auth CLI expects the Prisma client in its default location. To fix this, remove the output field from your generator client block in schema.prisma, re-generate the Prisma client, and update your imports to use import { PrismaClient } from '@prisma/client' instead of a relative path. Then, re-run npx @better-auth/cli generate and it should work without the adapter error. More details here: GitHub Issue #3736.

Also, make sure your auth instance is exported as a named export (export const auth = ...), not a default export—otherwise, the CLI can’t find it and will throw a similar error. See GitHub Issue #4110.

If you need to keep a custom output path for Prisma, let me know—there are some workarounds, but the default setup is the most reliable for Better Auth CLI.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Sep 15, 2025): <!-- Greeting --> Hi @JohnKesko! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You’re not missing anything obvious—this error is usually caused by the custom output path in your Prisma client generator. The Better Auth CLI expects the Prisma client in its default location. To fix this, remove the `output` field from your `generator client` block in `schema.prisma`, re-generate the Prisma client, and update your imports to use `import { PrismaClient } from '@prisma/client'` instead of a relative path. Then, re-run `npx @better-auth/cli generate` and it should work without the adapter error. More details here: [GitHub Issue #3736](https://github.com/better-auth/better-auth/issues/3736). Also, make sure your `auth` instance is exported as a named export (`export const auth = ...`), not a default export—otherwise, the CLI can’t find it and will throw a similar error. See [GitHub Issue #4110](https://github.com/better-auth/better-auth/issues/4110). If you need to keep a custom output path for Prisma, let me know—there are some workarounds, but the default setup is the most reliable for Better Auth CLI. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/eabef750-ba1d-42c0-ae26-129cb4bf0636?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4670)
Author
Owner

@JohnKesko commented on GitHub (Sep 15, 2025):

duplicate #3736

@JohnKesko commented on GitHub (Sep 15, 2025): duplicate #3736
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1929