Client bundle includes server-side dependencies (kysely, db adapters) when using only client imports #2638

Closed
opened 2026-03-13 10:10:03 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @cantutar on GitHub (Jan 1, 2026).

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When importing only client-side modules from better-auth, the client bundle includes server-side dependencies that should never be in a browser bundle.
Imports used
import {
adminClient,
customSessionClient,
inferAdditionalFields,
lastLoginMethodClient,
organizationClient,
twoFactorClient,
} from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";

Current vs. Expected behavior

My client bundle (1.6MB / 443KB gzip) includes:

  • kysely query-builder, dialects, parser, schema
  • better-auth server code: routes, db, adapter, api
  • bun-sqlite-dialect, node-sqlite-dialect (separate chunks created)
  • All zod locales
Image

Expected behavior
Client-only imports (better-auth/client/*, better-auth/react) should not pull in server-side dependencies like kysely, database adapters, or server routes.

What version of Better Auth are you using?

v1.4.10

System info

can@can-MS-7A32:~/Masaüstü/coding/fluctur$ bunx @better-auth/cli info --json
{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#37~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 20 10:25:38 UTC 2",
    "release": "6.14.0-37-generic",
    "cpuCount": 16,
    "cpuModel": "AMD Ryzen 7 5800X3D 8-Core Processor",
    "totalMemory": "31.26 GB",
    "freeMemory": "13.10 GB"
  },
  "node": {
    "version": "v22.19.0",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.3.5"
  },
  "frameworks": null,
  "databases": null,
  "betterAuth": {
    "version": "Unknown",
    "config": null
  }
}
can@can-MS-7A32:~/Masaüstü/coding/fluctur$

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

Client, Types, Package

Auth config (if applicable)

Auth config (server-side)
// Server: using better-auth/minimal
import { betterAuth } from "better-auth/minimal";
import {
  admin,
  captcha,
  customSession,
  lastLoginMethod,
  openAPI,
} from "better-auth/plugins";
export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg" }),
  plugins: [
    admin(),
    lastLoginMethod({ storeInDatabase: true }),
    twoFactor(),
    organization(),
    openAPI(),
    captcha({ provider: "cloudflare-turnstile", secretKey: "..." }),
    customSession(...),
  ],
  // ... other config
});
Client config
// Client: React SPA
import {
  adminClient,
  customSessionClient,
  inferAdditionalFields,
  lastLoginMethodClient,
  organizationClient,
  twoFactorClient,
} from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
  baseURL: "https://api.example.com",
  plugins: [
    inferAdditionalFields<typeof auth>(),
    customSessionClient<typeof auth>(),
    adminClient(),
    organizationClient(),
    twoFactorClient(),
    lastLoginMethodClient(),
  ],
});
Note: Server uses better-auth/minimal (no kysely), but client bundle still includes kysely and server code.

Additional context

No response

Originally created by @cantutar on GitHub (Jan 1, 2026). Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce When importing only client-side modules from better-auth, the client bundle includes server-side dependencies that should never be in a browser bundle. Imports used import { adminClient, customSessionClient, inferAdditionalFields, lastLoginMethodClient, organizationClient, twoFactorClient, } from "better-auth/client/plugins"; import { createAuthClient } from "better-auth/react"; ### Current vs. Expected behavior My client bundle (1.6MB / 443KB gzip) includes: - kysely query-builder, dialects, parser, schema - better-auth server code: routes, db, adapter, api - bun-sqlite-dialect, node-sqlite-dialect (separate chunks created) - All zod locales <img width="1929" height="998" alt="Image" src="https://github.com/user-attachments/assets/b6d35083-02d0-4553-8947-6e7ccc5dac96" /> Expected behavior Client-only imports (better-auth/client/*, better-auth/react) should not pull in server-side dependencies like kysely, database adapters, or server routes. ### What version of Better Auth are you using? v1.4.10 ### System info ```bash can@can-MS-7A32:~/Masaüstü/coding/fluctur$ bunx @better-auth/cli info --json { "system": { "platform": "linux", "arch": "x64", "version": "#37~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 20 10:25:38 UTC 2", "release": "6.14.0-37-generic", "cpuCount": 16, "cpuModel": "AMD Ryzen 7 5800X3D 8-Core Processor", "totalMemory": "31.26 GB", "freeMemory": "13.10 GB" }, "node": { "version": "v22.19.0", "env": "development" }, "packageManager": { "name": "bun", "version": "1.3.5" }, "frameworks": null, "databases": null, "betterAuth": { "version": "Unknown", "config": null } } can@can-MS-7A32:~/Masaüstü/coding/fluctur$ ``` ### Which area(s) are affected? (Select all that apply) Client, Types, Package ### Auth config (if applicable) ```typescript Auth config (server-side) // Server: using better-auth/minimal import { betterAuth } from "better-auth/minimal"; import { admin, captcha, customSession, lastLoginMethod, openAPI, } from "better-auth/plugins"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg" }), plugins: [ admin(), lastLoginMethod({ storeInDatabase: true }), twoFactor(), organization(), openAPI(), captcha({ provider: "cloudflare-turnstile", secretKey: "..." }), customSession(...), ], // ... other config }); Client config // Client: React SPA import { adminClient, customSessionClient, inferAdditionalFields, lastLoginMethodClient, organizationClient, twoFactorClient, } from "better-auth/client/plugins"; import { createAuthClient } from "better-auth/react"; export const authClient = createAuthClient({ baseURL: "https://api.example.com", plugins: [ inferAdditionalFields<typeof auth>(), customSessionClient<typeof auth>(), adminClient(), organizationClient(), twoFactorClient(), lastLoginMethodClient(), ], }); Note: Server uses better-auth/minimal (no kysely), but client bundle still includes kysely and server code. ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 10:10:03 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 1, 2026):

It seems that you're experiencing an issue where your client bundle includes server-side dependencies, such as Kysely, even when using only client imports from Better Auth. This can lead to an increased bundle size and unnecessary code being shipped to the client.

Next Steps

  1. Check Imports: Ensure that all your imports are strictly from better-auth/client and better-auth/react. Sometimes, inadvertently importing any server-side modules can pull in unwanted dependencies.

  2. Use better-auth/minimal: Consider using the better-auth/minimal package if you're not utilizing Kysely or need to minimize your bundle size. This version is designed to exclude layouts and dependencies that are not needed for client-side operations, ensuring a leaner bundle size [8].

  3. Report: If you've confirmed that your imports are correct and the issue persists, it might be beneficial to create a GitHub issue. Make sure to include specific details about your project setup, including the versions of Better Auth and any relevant dependencies.

You can tag @better-auth-agent if you wish to get a response from the agent about this situation. Is there anything specific you'd like to know more about in terms of optimizing your bundle size or using Better Auth?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 1, 2026): It seems that you're experiencing an issue where your client bundle includes server-side dependencies, such as Kysely, even when using only client imports from Better Auth. This can lead to an increased bundle size and unnecessary code being shipped to the client. ### Next Steps 1. **Check Imports**: Ensure that all your imports are strictly from `better-auth/client` and `better-auth/react`. Sometimes, inadvertently importing any server-side modules can pull in unwanted dependencies. 2. **Use `better-auth/minimal`**: Consider using the `better-auth/minimal` package if you're not utilizing Kysely or need to minimize your bundle size. This version is designed to exclude layouts and dependencies that are not needed for client-side operations, ensuring a leaner bundle size [[8]](https://github.com/better-auth/better-auth/pull/5690). 3. **Report**: If you've confirmed that your imports are correct and the issue persists, it might be beneficial to create a GitHub issue. Make sure to include specific details about your project setup, including the versions of Better Auth and any relevant dependencies. You can tag @better-auth-agent if you wish to get a response from the agent about this situation. Is there anything specific you'd like to know more about in terms of optimizing your bundle size or using Better Auth? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@dosubot[bot] commented on GitHub (Jan 1, 2026):

This is a known issue with better-auth: even when you use only client imports, server-side dependencies like kysely and db adapters can leak into your client bundle. The root cause is that the client entrypoints sometimes re-export types or modules from server code, and some plugins (like organization, admin, twoFactor) bundle both server and client logic together. If your bundler (Bun, Vite, etc.) doesn't fully respect type-only imports or tree-shaking, it may include server code in the client build example.

How to avoid this:

  • Audit your imports: only use better-auth/client and better-auth/client/plugins in client code. Never import from better-auth, better-auth/plugins, or any server adapter/config in client files example.
  • For shared types, put them in separate files that do not import server code or adapters example.
  • Use explicit type-only imports (import type { ... }) for types, not values, to help bundlers skip server code example.
  • If you use Vite, exclude better-auth from dependency optimization:
    optimizeDeps: { exclude: ['better-auth'] }
    
    For Bun or Next.js, you may need to treat better-auth as an external package in your config example.

Why this happens:
The package structure tries to separate client/server code, but due to re-exports and how plugins are implemented, server code can still leak into client bundles. Fixing this would require refactoring the client entrypoints to avoid re-exporting server modules and only expose client-safe code example.

Current state:
Some newer beta versions after v1.4.10 reportedly improve this, but the problem persists for some setups and bundlers. Making server dependencies like kysely truly optional is still under discussion example.

If you want help auditing your imports or have a minimal reproduction, I can help pinpoint the leak. Let me know if you want to dig into Bun-specific config or recent changes!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 1, 2026): <!-- Answer --> This is a known issue with better-auth: even when you use only client imports, server-side dependencies like kysely and db adapters can leak into your client bundle. The root cause is that the client entrypoints sometimes re-export types or modules from server code, and some plugins (like organization, admin, twoFactor) bundle both server and client logic together. If your bundler (Bun, Vite, etc.) doesn't fully respect type-only imports or tree-shaking, it may include server code in the client build [example](https://github.com/better-auth/better-auth/issues/5539). **How to avoid this:** - Audit your imports: only use `better-auth/client` and `better-auth/client/plugins` in client code. Never import from `better-auth`, `better-auth/plugins`, or any server adapter/config in client files [example](https://github.com/better-auth/better-auth/issues/3498). - For shared types, put them in separate files that do not import server code or adapters [example](https://github.com/better-auth/better-auth/issues/3136). - Use explicit type-only imports (`import type { ... }`) for types, not values, to help bundlers skip server code [example](https://github.com/better-auth/better-auth/issues/4457). - If you use Vite, exclude better-auth from dependency optimization: ```js optimizeDeps: { exclude: ['better-auth'] } ``` For Bun or Next.js, you may need to treat better-auth as an external package in your config [example](https://github.com/better-auth/better-auth/issues/6781). **Why this happens:** The package structure tries to separate client/server code, but due to re-exports and how plugins are implemented, server code can still leak into client bundles. Fixing this would require refactoring the client entrypoints to avoid re-exporting server modules and only expose client-safe code [example](https://github.com/better-auth/better-auth/issues/5539). **Current state:** Some newer beta versions after v1.4.10 reportedly improve this, but the problem persists for some setups and bundlers. Making server dependencies like kysely truly optional is still under discussion [example](https://github.com/better-auth/better-auth/issues/6183). If you want help auditing your imports or have a minimal reproduction, I can help pinpoint the leak. Let me know if you want to dig into Bun-specific config or recent changes! <!-- 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/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20known%20issue%20with%20better-auth%3A%20even%20when%20you%20use%20only%20client%20imports%2C%20server-side%20dependencies%20like%20kysely%20and%20db%20adapters%20can%20leak%20into%20your%20client%20bundle.%20The%20root%20cause%20is%20that%20the%20client%20entrypoints%20sometimes%20re-export%20types%20or%20modules%20from%20server%20code%2C%20and%20some%20plugins%20%28like%20organization%2C%20admin%2C%20twoFactor%29%20bundle%20both%20server%20and%20client%20logic%20together.%20If%20your%20bundler%20%28Bun%2C%20Vite%2C%20etc.%29%20doesn%27t%20fully%20respect%20type-only%20imports%20or%20tree-shaking%2C%20it%20may%20include%20server%20code%20in%20the%20client%20build%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/5539%29.%0A%0A%2A%2AHow%20to%20avoid%20this%3A%2A%2A%0A-%20Audit%20your%20imports%3A%20only%20use%20%60better-auth/client%60%20and%20%60better-auth/client/plugins%60%20in%20client%20code.%20Never%20import%20from%20%60better-auth%60%2C%20%60better-auth/plugins%60%2C%20or%20any%20server%20adapter/config%20in%20client%20files%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/3498%29.%0A-%20For%20shared%20types%2C%20put%20them%20in%20separate%20files%20that%20do%20not%20import%20server%20code%20or%20adapters%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/3136%29.%0A-%20Use%20explicit%20type-only%20imports%20%28%60import%20type%20%7B%20...%20%7D%60%29%20for%20types%2C%20not%20values%2C%20to%20help%20bundlers%20skip%20server%20code%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/4457%29.%0A-%20If%20you%20use%20Vite%2C%20exclude%20better-auth%20from%20dependency%20optimization%3A%20%20%0A%20%20%60%60%60js%0A%20%20optimizeDeps%3A%20%7B%20exclude%3A%20%5B%27better-auth%27%5D%20%7D%0A%20%20%60%60%60%0A%20%20For%20Bun%20or%20Next.js%2C%20you%20may%20need%20to%20treat%20better-auth%20as%20an%20external%20package%20in%20your%20config%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/6781%29.%0A%0A%2A%2AWhy%20this%20happens%3A%2A%2A%20%20%0AThe%20package%20structure%20tries%20to%20separate%20client/server%20code%2C%20but%20due%20to%20re-exports%20and%20how%20plugins%20are%20implemented%2C%20server%20code%20can%20still%20leak%20into%20client%20bundles.%20Fixing%20this%20would%20require%20refactoring%20the%20client%20entrypoints%20to%20avoid%20re-exporting%20server%20modules%20and%20only%20expose%20client-safe%20code%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/5539%29.%0A%0A%2A%2ACurrent%20state%3A%2A%2A%20%20%0ASome%20newer%20beta%20versions%20after%20v1.4.10%20reportedly%20improve%20this%2C%20but%20the%20problem%20persists%20for%20some%20setups%20and%20bundlers.%20Making%20server%20dependencies%20like%20kysely%20truly%20optional%20is%20still%20under%20discussion%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/6183%29.%0A%0AIf%20you%20want%20help%20auditing%20your%20imports%20or%20have%20a%20minimal%20reproduction%2C%20I%20can%20help%20pinpoint%20the%20leak.%20Let%20me%20know%20if%20you%20want%20to%20dig%20into%20Bun-specific%20config%20or%20recent%20changes%21)&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/7085)
Author
Owner

@cantutar commented on GitHub (Jan 1, 2026):

This is a known issue with better-auth: even when you use only client imports, server-side dependencies like kysely and db adapters can leak into your client bundle. The root cause is that the client entrypoints sometimes re-export types or modules from server code, and some plugins (like organization, admin, twoFactor) bundle both server and client logic together. If your bundler (Bun, Vite, etc.) doesn't fully respect type-only imports or tree-shaking, it may include server code in the client build example.

How to avoid this:

  • Audit your imports: only use better-auth/client and better-auth/client/plugins in client code. Never import from better-auth, better-auth/plugins, or any server adapter/config in client files example.

  • For shared types, put them in separate files that do not import server code or adapters example.

  • Use explicit type-only imports (import type { ... }) for types, not values, to help bundlers skip server code example.

  • If you use Vite, exclude better-auth from dependency optimization:
    optimizeDeps: { exclude: ['better-auth'] }

    For Bun or Next.js, you may need to treat better-auth as an external package in your config example.

Why this happens: The package structure tries to separate client/server code, but due to re-exports and how plugins are implemented, server code can still leak into client bundles. Fixing this would require refactoring the client entrypoints to avoid re-exporting server modules and only expose client-safe code example.

Current state: Some newer beta versions after v1.4.10 reportedly improve this, but the problem persists for some setups and bundlers. Making server dependencies like kysely truly optional is still under discussion example.

If you want help auditing your imports or have a minimal reproduction, I can help pinpoint the leak. Let me know if you want to dig into Bun-specific config or recent changes!

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

I tried optimizeDeps: { exclude: ['better-auth'] } but it didn't help. also tried to exlcude the better-auth/api it dropped 800kb which is half of the bundle size

@cantutar commented on GitHub (Jan 1, 2026): > This is a known issue with better-auth: even when you use only client imports, server-side dependencies like kysely and db adapters can leak into your client bundle. The root cause is that the client entrypoints sometimes re-export types or modules from server code, and some plugins (like organization, admin, twoFactor) bundle both server and client logic together. If your bundler (Bun, Vite, etc.) doesn't fully respect type-only imports or tree-shaking, it may include server code in the client build [example](https://github.com/better-auth/better-auth/issues/5539). > > **How to avoid this:** > > * Audit your imports: only use `better-auth/client` and `better-auth/client/plugins` in client code. Never import from `better-auth`, `better-auth/plugins`, or any server adapter/config in client files [example](https://github.com/better-auth/better-auth/issues/3498). > * For shared types, put them in separate files that do not import server code or adapters [example](https://github.com/better-auth/better-auth/issues/3136). > * Use explicit type-only imports (`import type { ... }`) for types, not values, to help bundlers skip server code [example](https://github.com/better-auth/better-auth/issues/4457). > * If you use Vite, exclude better-auth from dependency optimization: > optimizeDeps: { exclude: ['better-auth'] } > > > For Bun or Next.js, you may need to treat better-auth as an external package in your config [example](https://github.com/better-auth/better-auth/issues/6781). > > **Why this happens:** The package structure tries to separate client/server code, but due to re-exports and how plugins are implemented, server code can still leak into client bundles. Fixing this would require refactoring the client entrypoints to avoid re-exporting server modules and only expose client-safe code [example](https://github.com/better-auth/better-auth/issues/5539). > > **Current state:** Some newer beta versions after v1.4.10 reportedly improve this, but the problem persists for some setups and bundlers. Making server dependencies like kysely truly optional is still under discussion [example](https://github.com/better-auth/better-auth/issues/6183). > > If you want help auditing your imports or have a minimal reproduction, I can help pinpoint the leak. Let me know if you want to dig into Bun-specific config or recent changes! > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/73ae2b49-b53d-4667-8c8e-7a79b0270513?feedback_type=other) > > [![Chat with Dosu](https://camo.githubusercontent.com/7c571478962a6b8a5d47cac74b6824c75e4f6731bba52377e6007f9150462923/68747470733a2f2f646f73752e6465762f646f73752d636861742d62616467652e737667)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github) [![Open in Cursor](https://camo.githubusercontent.com/bdf7dbded65ee585887f47e5fd6fa099ea0933088b4b03d02e83b4def50154d4/68747470733a2f2f646f73752e6465762f646f73752d696e2d637572736f722e737667)](https://cursor.com/link/prompt?text=This%20is%20a%20known%20issue%20with%20better-auth%3A%20even%20when%20you%20use%20only%20client%20imports%2C%20server-side%20dependencies%20like%20kysely%20and%20db%20adapters%20can%20leak%20into%20your%20client%20bundle.%20The%20root%20cause%20is%20that%20the%20client%20entrypoints%20sometimes%20re-export%20types%20or%20modules%20from%20server%20code%2C%20and%20some%20plugins%20%28like%20organization%2C%20admin%2C%20twoFactor%29%20bundle%20both%20server%20and%20client%20logic%20together.%20If%20your%20bundler%20%28Bun%2C%20Vite%2C%20etc.%29%20doesn%27t%20fully%20respect%20type-only%20imports%20or%20tree-shaking%2C%20it%20may%20include%20server%20code%20in%20the%20client%20build%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/5539%29.%0A%0A%2A%2AHow%20to%20avoid%20this%3A%2A%2A%0A-%20Audit%20your%20imports%3A%20only%20use%20%60better-auth/client%60%20and%20%60better-auth/client/plugins%60%20in%20client%20code.%20Never%20import%20from%20%60better-auth%60%2C%20%60better-auth/plugins%60%2C%20or%20any%20server%20adapter/config%20in%20client%20files%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/3498%29.%0A-%20For%20shared%20types%2C%20put%20them%20in%20separate%20files%20that%20do%20not%20import%20server%20code%20or%20adapters%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/3136%29.%0A-%20Use%20explicit%20type-only%20imports%20%28%60import%20type%20%7B%20...%20%7D%60%29%20for%20types%2C%20not%20values%2C%20to%20help%20bundlers%20skip%20server%20code%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/4457%29.%0A-%20If%20you%20use%20Vite%2C%20exclude%20better-auth%20from%20dependency%20optimization%3A%20%20%0A%20%20%60%60%60js%0A%20%20optimizeDeps%3A%20%7B%20exclude%3A%20%5B%27better-auth%27%5D%20%7D%0A%20%20%60%60%60%0A%20%20For%20Bun%20or%20Next.js%2C%20you%20may%20need%20to%20treat%20better-auth%20as%20an%20external%20package%20in%20your%20config%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/6781%29.%0A%0A%2A%2AWhy%20this%20happens%3A%2A%2A%20%20%0AThe%20package%20structure%20tries%20to%20separate%20client/server%20code%2C%20but%20due%20to%20re-exports%20and%20how%20plugins%20are%20implemented%2C%20server%20code%20can%20still%20leak%20into%20client%20bundles.%20Fixing%20this%20would%20require%20refactoring%20the%20client%20entrypoints%20to%20avoid%20re-exporting%20server%20modules%20and%20only%20expose%20client-safe%20code%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/5539%29.%0A%0A%2A%2ACurrent%20state%3A%2A%2A%20%20%0ASome%20newer%20beta%20versions%20after%20v1.4.10%20reportedly%20improve%20this%2C%20but%20the%20problem%20persists%20for%20some%20setups%20and%20bundlers.%20Making%20server%20dependencies%20like%20kysely%20truly%20optional%20is%20still%20under%20discussion%C2%A0%5Bexample%5D%28https%3A//github.com/better-auth/better-auth/issues/6183%29.%0A%0AIf%20you%20want%20help%20auditing%20your%20imports%20or%20have%20a%20minimal%20reproduction%2C%20I%20can%20help%20pinpoint%20the%20leak.%20Let%20me%20know%20if%20you%20want%20to%20dig%20into%20Bun-specific%20config%20or%20recent%20changes%21) [![Join Discord](https://camo.githubusercontent.com/7d2066700925db1e370d956834f9ea0e5bc92cecbe84963fbd2a363e45091b4e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/a8bd7f3fcf3f5e3bd124eee9ae50ae2ac17b40971726665ac2121d48f9aee155/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/7085) I tried optimizeDeps: { exclude: ['better-auth'] } but it didn't help. also tried to exlcude the better-auth/api it dropped 800kb which is half of the bundle size
Author
Owner

@bytaesu commented on GitHub (Jan 8, 2026):

Hi @cantutar,

I'm looking into this!

@bytaesu commented on GitHub (Jan 8, 2026): Hi @cantutar, I'm looking into this!
Author
Owner

@bytaesu commented on GitHub (Jan 8, 2026):

Hello @cantutar,

Since I introduced better-auth/minimal with the v1.4 release, I wanted to look into this issue as well.

I tried testing it in this repo by only modifying the client plugins, but I wasn't able to reproduce the problem. Could you share a simple repo where it can be reproduced?

It might be caused by server-side code being incorrectly imported on the client-side or an issue with a specific bundler.

Reference

  • Framework: TanStack Start
  • Version: better-auth v1.4.10
import {createAuthClient} from 'better-auth/react'
import {
	adminClient,
	customSessionClient,
	inferAdditionalFields,
	lastLoginMethodClient,
	organizationClient,
	twoFactorClient,
} from "better-auth/client/plugins";

export const authClient = createAuthClient({
	plugins: [
		inferAdditionalFields(),
		customSessionClient(),
		adminClient(),
		organizationClient(),
		twoFactorClient(),
		lastLoginMethodClient(),
	],
})
Image
@bytaesu commented on GitHub (Jan 8, 2026): Hello @cantutar, Since I introduced `better-auth/minimal` with the v1.4 release, I wanted to look into this issue as well. I tried testing it in [this repo](https://github.com/bytaesu/temp-tanstack-betterauth-bun) by only modifying the client plugins, but I wasn't able to reproduce the problem. Could you share a simple repo where it can be reproduced? It might be caused by server-side code being incorrectly imported on the client-side or an issue with a specific bundler. ### Reference - Framework: TanStack Start - Version: better-auth v1.4.10 ```ts import {createAuthClient} from 'better-auth/react' import { adminClient, customSessionClient, inferAdditionalFields, lastLoginMethodClient, organizationClient, twoFactorClient, } from "better-auth/client/plugins"; export const authClient = createAuthClient({ plugins: [ inferAdditionalFields(), customSessionClient(), adminClient(), organizationClient(), twoFactorClient(), lastLoginMethodClient(), ], }) ``` <img width="993" height="948" alt="Image" src="https://github.com/user-attachments/assets/0060cb57-f7fb-47b1-95f6-0da4cf2d7ab4" />
Author
Owner

@bytaesu commented on GitHub (Jan 8, 2026):

We have a https://github.com/better-auth/better-auth/tree/canary/e2e/smoke/test/fixtures/vite that bundles client-side entry with treeshake: false, and it doesn't include any server-side dependencies.

I think I could not reproduce this issue. If the problem persists, please mention me or feel free to reopen this issue 🙏

@bytaesu commented on GitHub (Jan 8, 2026): We have a https://github.com/better-auth/better-auth/tree/canary/e2e/smoke/test/fixtures/vite that bundles client-side entry with `treeshake: false`, and it doesn't include any server-side dependencies. I think I could not reproduce this issue. If the problem persists, please mention me or feel free to reopen this issue 🙏
Author
Owner

@cantutar commented on GitHub (Jan 8, 2026):

I guess so i might found the problem was polars plugin then fixed using polar client

@cantutar commented on GitHub (Jan 8, 2026): I guess so i might found the problem was polars plugin then fixed using polar client
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2638