Can't use @better-auth/cli when exporting a auth function

I am using Better Auth across my api (hono + cloudflare workers) and fronted (nextjs + vercel), so far so good however when i want to run the bunx @better-auth/cli generate i get the following error:
$ bunx @better-auth/cli generate --config ../api/src/common/auth.ts --output ./src/schemas/auth.ts
ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config in BuzzTrip/packages/api/src/common/auth.ts. Make sure to default export your auth instance or to export as a variable named auth.
error: script "db:auth" exited with code 1

my auth config is setup like so:
export const createAuth = (
  baseUrl: string,
  authSecret: string,
  googleClientId: string,
  googleClientSecret: string,
  url: string,
  authToken: string
) => {
  const db = createDb(url, authToken);

  return betterAuth({
    // ... config
  });
};

export type Auth = ReturnType<typeof createAuth>;


Since we need to pass in env's from the cloudflare context, we need to create the auth client rather than just existing.

my repo: https://github.com/jacobsamo/BuzzTrip
branch: https://github.com/jacobsamo/BuzzTrip/tree/feat/migrate-to-better-auth
code causing the problem: https://github.com/jacobsamo/BuzzTrip/blob/feat/migrate-to-better-auth/apps/api/src/common/auth.ts
run from: https://github.com/jacobsamo/BuzzTrip/blob/feat/migrate-to-better-auth/packages/db/package.json
GitHub
Plan the trip you've always dreamed of. Contribute to jacobsamo/BuzzTrip development by creating an account on GitHub.
GitHub
Plan the trip you've always dreamed of. Contribute to jacobsamo/BuzzTrip development by creating an account on GitHub.
GitHub
Plan the trip you've always dreamed of. Contribute to jacobsamo/BuzzTrip development by creating an account on GitHub.
GitHub
Plan the trip you've always dreamed of. Contribute to jacobsamo/BuzzTrip development by creating an account on GitHub.
Solution
you can call
import { getMigrations } from "better-auth/db";

const { runMigrations } = await getMigrtions(auth.options);
await runMigratinos() //this will run the migrations for you
Was this page helpful?