[GH-ISSUE #2207] Defining a Prisma client output breaks the CLI generate #9104

Closed
opened 2026-04-13 04:26:07 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @pcamarajr on GitHub (Apr 9, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2207

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  • Start a fresh next.js application: npx create-next-app@latest
  • Init prisma with output definition: npx prisma init --output ../src/app/generated/prisma
  • Add a foo model to prisma and generate: npx prisma generate
  • Init Better Auth: npx @better-auth/cli@latest init

Use the prisma client within a prismaAdapter

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "@/lib/prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  emailAndPassword: {  
    enabled: true
  },
})
  • Finally run better-auth/cli generate: npx @better-auth/cli@latest generate

Current vs. Expected behavior

Current

Better Auth does not find the generated prisma client and throws an error

git:(main) ✗ npx @better-auth/cli@latest generate
2025-04-09T20:28:26.829Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

Expected

The Better Auth models are successfully generated in our prism.schema

What version of Better Auth are you using?

1.2.5

Provide environment information

- OS: macOS Sequoia

Which area(s) are affected? (Select all that apply)

CLI, generate command

Additional context

Removing the Prisma output path fixes the problem, which leads me to believe that Better Auth is not looking at the output path from prisma.schema.

Originally created by @pcamarajr on GitHub (Apr 9, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2207 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce - **Start a fresh next.js application**: `npx create-next-app@latest` - **Init prisma with output definition**: `npx prisma init --output ../src/app/generated/prisma` - **Add a foo model to prisma and generate**: `npx prisma generate` - **Init Better Auth**: `npx @better-auth/cli@latest init` Use the prisma client within a `prismaAdapter` ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { prisma } from "@/lib/prisma"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true }, }) ``` - **Finally run better-auth/cli generate**: `npx @better-auth/cli@latest generate` ### Current vs. Expected behavior #### Current Better Auth does not find the generated prisma client and throws an error ```bash git:(main) ✗ npx @better-auth/cli@latest generate 2025-04-09T20:28:26.829Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again. ``` #### Expected The Better Auth models are successfully generated in our `prism.schema` ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash - OS: macOS Sequoia ``` ### Which area(s) are affected? (Select all that apply) CLI, generate command ### Additional context Removing the Prisma `output` path fixes the problem, which leads me to believe that Better Auth is not looking at the `output` path from `prisma.schema`.
GiteaMirror added the locked label 2026-04-13 04:26:07 -05:00
Author
Owner

@ReinforceZwei commented on GitHub (Apr 10, 2025):

I have faced the exact same problem as you and found the way to solve the problem. The error message should be coming from @prisma/client but not better auth. You might be importing prisma client from a wrong location.

For my case, my prisma schema looks like:

generator client {
  provider = "prisma-client-js"
  output   = "../src/generated/prisma"
}

Notice the output directory of the generated client. In auth.ts, import the generated client from that directory instead of @prisma/client:

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "@/generated/prisma/client"; // import from generated output
 
const prisma = new PrismaClient();
export const auth = betterAuth({
    database: prismaAdapter(prisma, {
        provider: "postgresql",
    }),
});

See generating Prisma Client about the output option.


In your case, I can see you set the prisma output to ../src/app/generated/prisma. However, you are importing the client from @/lib/prisma in your code. Perhaps you are importing prisma client from the wrong location?

<!-- gh-comment-id:2791707173 --> @ReinforceZwei commented on GitHub (Apr 10, 2025): I have faced the exact same problem as you and found the way to solve the problem. The error message should be coming from `@prisma/client` but not better auth. You might be importing prisma client from a wrong location. For my case, my prisma schema looks like: ```prisma generator client { provider = "prisma-client-js" output = "../src/generated/prisma" } ``` Notice the output directory of the generated client. In `auth.ts`, import the generated client from that directory instead of `@prisma/client`: ```ts import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@/generated/prisma/client"; // import from generated output const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), }); ``` See [generating Prisma Client](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client?utm_source=CLI&utm_medium=generator-warning) about the `output` option. *** In your case, I can see you set the prisma output to `../src/app/generated/prisma`. However, you are importing the client from `@/lib/prisma` in your code. Perhaps you are importing prisma client from the wrong location?
Author
Owner

@Kinfe123 commented on GitHub (Apr 10, 2025):

i guess you have to update your Better Auth configuration to use the correct import of the prisma client that you have generate if u do have custom path for the output config with prisma file

<!-- gh-comment-id:2791761820 --> @Kinfe123 commented on GitHub (Apr 10, 2025): i guess you have to update your Better Auth configuration to use the correct import of the prisma client that you have generate if u do have custom path for the output config with prisma file
Author
Owner

@Mateleo commented on GitHub (Apr 10, 2025):

EDIT: I found the issue replace import { PrismaClient } from "@/generated/prisma/client"; by import { PrismaClient } from "../../generated/prisma/client"; (don't use @ or ~)

I'm facing the same issue, yet everything is working fine Prisma wise.
I'm using Nuxt

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "@/generated/prisma/client";

const prisma = new PrismaClient();
export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
});

PS C:\Path\To\Project> npx @better-auth/cli generate --config .\server\utils\auth.ts
YYYY-MM-DDTHH:MM:SS.MSZ ERROR [Better Auth]: Couldn't read your auth config. Error: Cannot find module '@/generated/prisma/client'
Require stack:
- C:\Path\To\Project\server\utils\prisma.ts
    at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Function.resolve (node:internal/modules/helpers:146:19)
    at jitiResolve (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:187220)
    at jitiRequire (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:189288)
    at import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199778)
    at C:/Path/To/Project/server/utils/prisma.ts:1:217
    at eval_evalModule (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:196325)
    at jitiRequire (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:190233)
    at import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199778)
    at C:/Path/To/Project/server/utils/auth.ts:3:45
    at async Function.import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199772)
    at async resolveConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:346:18)
    at async loadConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:146:23)
    at async getConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/@better-auth/cli/dist/index.mjs:213:26)
    at async Command.generateAction (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/@better-auth/cli/dist/index.mjs:643:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Path\\To\\Project\\server\\utils\\prisma.ts' ]
}
<!-- gh-comment-id:2792081461 --> @Mateleo commented on GitHub (Apr 10, 2025): EDIT: I found the issue replace `import { PrismaClient } from "@/generated/prisma/client";` by `import { PrismaClient } from "../../generated/prisma/client";` (don't use `@` or `~`) I'm facing the same issue, yet everything is working fine Prisma wise. I'm using Nuxt ```ts import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@/generated/prisma/client"; const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), }); ```` ```sh PS C:\Path\To\Project> npx @better-auth/cli generate --config .\server\utils\auth.ts YYYY-MM-DDTHH:MM:SS.MSZ ERROR [Better Auth]: Couldn't read your auth config. Error: Cannot find module '@/generated/prisma/client' Require stack: - C:\Path\To\Project\server\utils\prisma.ts at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15) at Function.resolve (node:internal/modules/helpers:146:19) at jitiResolve (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:187220) at jitiRequire (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:189288) at import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199778) at C:/Path/To/Project/server/utils/prisma.ts:1:217 at eval_evalModule (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:196325) at jitiRequire (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:190233) at import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199778) at C:/Path/To/Project/server/utils/auth.ts:3:45 at async Function.import (C:\Users\User\AppData\Local\npm-cache\_npx\HASH\node_modules\jiti\dist\jiti.cjs:1:199772) at async resolveConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:346:18) at async loadConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:146:23) at async getConfig (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/@better-auth/cli/dist/index.mjs:213:26) at async Command.generateAction (file:///C:/Users/User/AppData/Local/npm-cache/_npx/HASH/node_modules/@better-auth/cli/dist/index.mjs:643:18) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\\Path\\To\\Project\\server\\utils\\prisma.ts' ] } ````
Author
Owner

@pcamarajr commented on GitHub (Apr 10, 2025):

@ReinforceZwei and @Kinfe123 you both were right. Thank you 🙏

I haven't noticed that the Prisma docs now recommends to use PrismaClient from generated.

Not sure if I was doing it wrong, but I always used it from @prisma/client, but changing the import woks.

// src/lib/prisma.ts
- import { PrismaClient } from '@prisma/client'
+ import { PrismaClient } from '@/generated/prisma'
<!-- gh-comment-id:2792233698 --> @pcamarajr commented on GitHub (Apr 10, 2025): @ReinforceZwei and @Kinfe123 you both were right. Thank you 🙏 I haven't noticed that the [Prisma docs](https://www.prisma.io/docs/guides/nextjs#21-install-prisma-orm-and-create-your-first-models) now recommends to use `PrismaClient` from `generated`. Not sure if I was doing it wrong, but I always used it from `@prisma/client`, but changing the import woks. ```diff // src/lib/prisma.ts - import { PrismaClient } from '@prisma/client' + import { PrismaClient } from '@/generated/prisma' ```
Author
Owner

@Kinfe123 commented on GitHub (Apr 10, 2025):

I'm going to add this somewhere in our docs also to avoid such confusion

<!-- gh-comment-id:2792237282 --> @Kinfe123 commented on GitHub (Apr 10, 2025): I'm going to add this somewhere in our docs also to avoid such confusion
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9104