[GH-ISSUE #1215] Couldn't read your auth config. using npx @better-auth/cli generate #8646

Closed
opened 2026-04-13 03:47:48 -05:00 by GiteaMirror · 12 comments
Owner

Originally created by @ejabu on GitHub (Jan 15, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1215

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Install better-auth
    "better-auth": "1.1.13",
  2. setup .env file, BETTER_AUTH_SECRET and BETTER_AUTH_URL
  3. npx prisma migrate dev

Current vs. Expected behavior

2025-01-15T05:50:14.557Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config.

2025-01-15T05:50:14.558Z INFO [Better Auth]: [#better-auth]: Make sure to default export your auth instance or to export as a variable named auth.

What version of Better Auth are you using?

1.1.13

Provide environment information

BETTER_AUTH_SECRET="bc941545-d147-42e1-b669-c99a20f28b71"
BETTER_AUTH_URL=http://localhost:3000

OS Linux
Chrome

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

Backend, Client

Auth config (if applicable)

import { betterAuth, type BetterAuthOptions } from "better-auth";
import { customSession } from "better-auth/plugins";

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

const prisma = new PrismaClient();
const options = {
  database: prismaAdapter(prisma, {
    provider: "postgresql", // or "mysql", "postgresql", ...etc
  }),
  emailAndPassword: {
    enabled: true,
    sendResetPassword: async ({ user, url, token }, request) => {
      console.log({
        to: user.email,
        subject: "Reset your password",
        text: `Click the link to reset your password: ${url}`,
      });
    },
  },
  socialProviders: {
    // google: {
    //   clientId: process.env.GOOGLE_CLIENT_ID!,
    //   clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    // },
  },
  plugins: [
    customSession(async ({ user, session }) => {
      return {
        user: {
          ...user,
        },
        session,
      };
    }),
  ],
} satisfies BetterAuthOptions;

export const auth = betterAuth({
  ...options,
  plugins: [
    ...options.plugins,
    customSession(async ({ user, session }) => {
      return {
        user: {
          ...user,
        },
        session,
      };
    }, options),
  ],
});
Originally created by @ejabu on GitHub (Jan 15, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1215 ### Is this suited for github? - [X] Yes, this is suited for github ### To Reproduce 1. Install better-auth "better-auth": "1.1.13", 2. setup .env file, BETTER_AUTH_SECRET and BETTER_AUTH_URL 3. npx prisma migrate dev ### Current vs. Expected behavior 2025-01-15T05:50:14.557Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. 2025-01-15T05:50:14.558Z INFO [Better Auth]: [#better-auth]: Make sure to default export your auth instance or to export as a variable named auth. ### What version of Better Auth are you using? 1.1.13 ### Provide environment information ```bash BETTER_AUTH_SECRET="bc941545-d147-42e1-b669-c99a20f28b71" BETTER_AUTH_URL=http://localhost:3000 OS Linux Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth, type BetterAuthOptions } from "better-auth"; import { customSession } from "better-auth/plugins"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); const options = { database: prismaAdapter(prisma, { provider: "postgresql", // or "mysql", "postgresql", ...etc }), emailAndPassword: { enabled: true, sendResetPassword: async ({ user, url, token }, request) => { console.log({ to: user.email, subject: "Reset your password", text: `Click the link to reset your password: ${url}`, }); }, }, socialProviders: { // google: { // clientId: process.env.GOOGLE_CLIENT_ID!, // clientSecret: process.env.GOOGLE_CLIENT_SECRET!, // }, }, plugins: [ customSession(async ({ user, session }) => { return { user: { ...user, }, session, }; }), ], } satisfies BetterAuthOptions; export const auth = betterAuth({ ...options, plugins: [ ...options.plugins, customSession(async ({ user, session }) => { return { user: { ...user, }, session, }; }, options), ], }); ```
GiteaMirror added the lockedbug labels 2026-04-13 03:47:49 -05:00
Author
Owner

@bezerrath commented on GitHub (Jan 17, 2025):

assuming your auth.ts file is in the correct place (ex: src/lib/auth.ts), make sure you're not doing this:

export const auth = betterAuth({
...
});

export default auth;

but this:

export const auth = betterAuth({
...
});
<!-- gh-comment-id:2598510246 --> @bezerrath commented on GitHub (Jan 17, 2025): assuming your auth.ts file is in the correct place (ex: src/lib/auth.ts), make sure you're not doing this: ❌ ```js export const auth = betterAuth({ ... }); export default auth; ``` but this: ```js export const auth = betterAuth({ ... }); ```
Author
Owner

@isaced commented on GitHub (Jan 18, 2025):

I encountered the same issue, but I temporarily removed the plugins defined in auth.ts and re-executed the command, and it worked.

<!-- gh-comment-id:2599538423 --> @isaced commented on GitHub (Jan 18, 2025): I encountered the same issue, but I temporarily removed the plugins defined in auth.ts and re-executed the command, and it worked.
Author
Owner

@juliusmarminge commented on GitHub (Feb 11, 2025):

getting this too:

Image

Image

Image

<!-- gh-comment-id:2652238074 --> @juliusmarminge commented on GitHub (Feb 11, 2025): getting this too: ![Image](https://github.com/user-attachments/assets/a6035425-1c0b-4db4-a07c-2304e3809dd7) ![Image](https://github.com/user-attachments/assets/07ee14ae-6e9f-4f35-87d5-699ca3b1ab77) ![Image](https://github.com/user-attachments/assets/c9c1f8a1-3696-4ae4-b399-83b81e08c851)
Author
Owner

@moshetanzer commented on GitHub (Mar 24, 2025):

Hey,

Not sure what exactly your setup is, however, you can always just pass --config flag for both generate and migrate CLI commands to specify where your config is

<!-- gh-comment-id:2747498724 --> @moshetanzer commented on GitHub (Mar 24, 2025): Hey, Not sure what exactly your setup is, however, you can always just pass `--config` flag for both `generate` and `migrate` CLI commands to specify where your config is
Author
Owner

@ejabu commented on GitHub (Mar 25, 2025):

assuming your auth.ts file is in the correct place (ex: src/lib/auth.ts), make sure you're not doing this:

export const auth = betterAuth({
...
});

export default auth;
but this:

export const auth = betterAuth({
...
});

Nice it worked perfectly. Thank you @bezerrath !

I'm closing this then.

<!-- gh-comment-id:2750195494 --> @ejabu commented on GitHub (Mar 25, 2025): > assuming your auth.ts file is in the correct place (ex: src/lib/auth.ts), make sure you're not doing this: > > ❌ > > export const auth = betterAuth({ > ... > }); > > export default auth; > but this: > > export const auth = betterAuth({ > ... > }); Nice it worked perfectly. Thank you @bezerrath ! I'm closing this then.
Author
Owner

@hoagy-davis-digges commented on GitHub (Jul 13, 2025):

The documentation and the cli response suggests that it is possible to export the auth instance as default, even though it doesn't work, it would be useful to have this clarified, or better yet made actually possible

<!-- gh-comment-id:3067106225 --> @hoagy-davis-digges commented on GitHub (Jul 13, 2025): The documentation and the cli response suggests that it is possible to export the auth instance as default, even though it doesn't work, it would be useful to have this clarified, or better yet made actually possible
Author
Owner

@Pankaj-birla commented on GitHub (Jul 28, 2025):

Image

auth.ts in my lib folder :
`import { db } from "@/drizzle/db";
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";

export const auth = betterAuth({
database: drizzleAdapter(db, { provider: "pg" }),
});
`
but getting the similar error : ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Option apiKey is required

Solution:
Hi Folks, I got what's the problem in my xata.ts (using xata as an postgresSql) file:
`// Generated by Xata Codegen 0.30.1. Please do not edit.
import 'dotenv/config';
import { buildClient } from "@xata.io/client";
import type { BaseClientOptions } from "@xata.io/client";

export type DatabaseSchema = {};

const DatabaseClient = buildClient();
console.log("XATA_API_KEY:", process.env.XATA_API_KEY);
const defaultOptions = {
databaseURL:
"DB_URL",
apiKey: process.env.XATA_API_KEY, // ← camelCase here in apiKey (which I am missing)
branch: 'main'
};

export class XataClient extends DatabaseClient {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options });
}
}

let instance: XataClient | undefined = undefined;

export const getXataClient = () => {
if (instance) return instance;

instance = new XataClient();
return instance;
};
`

<!-- gh-comment-id:3128424633 --> @Pankaj-birla commented on GitHub (Jul 28, 2025): <img width="309" height="791" alt="Image" src="https://github.com/user-attachments/assets/7a9c3059-f00e-4697-8c4b-14184704737a" /> auth.ts in my lib folder : `import { db } from "@/drizzle/db"; import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg" }), }); ` but getting the similar error : ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Option apiKey is required **Solution**: Hi Folks, I got what's the problem in my xata.ts (using xata as an postgresSql) file: `// Generated by Xata Codegen 0.30.1. Please do not edit. import 'dotenv/config'; import { buildClient } from "@xata.io/client"; import type { BaseClientOptions } from "@xata.io/client"; export type DatabaseSchema = {}; const DatabaseClient = buildClient(); console.log("XATA_API_KEY:", process.env.XATA_API_KEY); const defaultOptions = { databaseURL: "DB_URL", apiKey: process.env.XATA_API_KEY, // ← camelCase here in apiKey (which I am missing) branch: 'main' }; export class XataClient extends DatabaseClient<DatabaseSchema> { constructor(options?: BaseClientOptions) { super({ ...defaultOptions, ...options }); } } let instance: XataClient | undefined = undefined; export const getXataClient = () => { if (instance) return instance; instance = new XataClient(); return instance; }; `
Author
Owner

@mintydev789 commented on GitHub (Nov 30, 2025):

I encountered the same issue, but I temporarily removed the plugins defined in auth.ts and re-executed the command, and it worked.

Wait, so plugins affect the behavior of the generate command?

<!-- gh-comment-id:3592360643 --> @mintydev789 commented on GitHub (Nov 30, 2025): > I encountered the same issue, but I temporarily removed the plugins defined in auth.ts and re-executed the command, and it worked. Wait, so plugins affect the behavior of the generate command?
Author
Owner

@mintydev789 commented on GitHub (Nov 30, 2025):

Oh, in my case I had to temporarily remove the relations property in the drizzle db config. Not sure if it could have something to do with me using the Drizzle 1.0 beta.

<!-- gh-comment-id:3592367156 --> @mintydev789 commented on GitHub (Nov 30, 2025): Oh, in my case I had to temporarily remove the `relations` property in the drizzle db config. Not sure if it could have something to do with me using the Drizzle 1.0 beta.
Author
Owner

@till commented on GitHub (Dec 6, 2025):

We had a similar problem after we updated from 1.3 to 1.4:

npm warn exec The following package was not found and will be installed: @better-auth/cli@1.4.5
2025-12-06T09:41:38.594Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './plugins/passkey' is not defined by "exports" in /home/runner/work/auth-auth/auth-auth/node_modules/better-auth/package.json

What we needed to do was to install the passkey plugin: npm i @better-auth/passkey and update the import statement for it in the auth config to reflect the change (also in docs):

import { passkey } from "@better-auth/passkey";

Not a huge deal - but if you are changing functionality - don't be afraid of a new major version?

<!-- gh-comment-id:3619884230 --> @till commented on GitHub (Dec 6, 2025): We had a similar problem after we updated from 1.3 to 1.4: ``` npm warn exec The following package was not found and will be installed: @better-auth/cli@1.4.5 2025-12-06T09:41:38.594Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './plugins/passkey' is not defined by "exports" in /home/runner/work/auth-auth/auth-auth/node_modules/better-auth/package.json ``` What we needed to do was to install the passkey plugin: `npm i @better-auth/passkey` and update the import statement for it in the auth config to reflect the change (also in [docs](https://www.better-auth.com/docs/plugins/passkey)): ```js import { passkey } from "@better-auth/passkey"; ``` Not a huge deal - but if you are changing functionality - don't be afraid of a new major version?
Author
Owner

@eralvarez commented on GitHub (Dec 18, 2025):

generate command doesn't work with this lib/auth.ts file

I'm using a nextjs project in a wsl2 ubuntu env

import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
  database: new Database("../project.db"),
})
<!-- gh-comment-id:3667991354 --> @eralvarez commented on GitHub (Dec 18, 2025): `generate` command doesn't work with this lib/auth.ts file I'm using a nextjs project in a wsl2 ubuntu env ```typescript import { betterAuth } from "better-auth"; import Database from "better-sqlite3"; export const auth = betterAuth({ database: new Database("../project.db"), }) ```
Author
Owner

@eralvarez commented on GitHub (Dec 18, 2025):

generate command doesn't work with this lib/auth.ts file

I'm using a nextjs project in a wsl2 ubuntu env

import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
database: new Database("../project.db"),
})

Error:

ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module '/<pc-path>/<project>/<pc-path>/<project>/lib/auth.ts'

for some reason it is duplicating the path even if I pass the --config flag, my workaround was to generate the whole path it is expecting in my project 🤦‍♂️

<!-- gh-comment-id:3668009298 --> @eralvarez commented on GitHub (Dec 18, 2025): > `generate` command doesn't work with this lib/auth.ts file > > I'm using a nextjs project in a wsl2 ubuntu env > > import { betterAuth } from "better-auth"; > import Database from "better-sqlite3"; > > export const auth = betterAuth({ > database: new Database("../project.db"), > }) Error: ``` ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: Cannot find module '/<pc-path>/<project>/<pc-path>/<project>/lib/auth.ts' ``` for some reason it is duplicating the path even if I pass the --config flag, my workaround was to generate the whole path it is expecting in my project 🤦‍♂️
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8646