.postmortem/client-side-import-server.md
Users are incorrectly importing the server-side better-auth package
directly in their client-side code, causing build and runtime errors.
This is a common user mistake that keeps resurfacing.
Users create this incorrect dependency chain in their client code:
loginPage (React/Vue/Solid component)
↓
auth-client.ts
↓
import { ... } from "better-auth" ← WRONG!
This is 100% a user error. The correct import should be:
// WRONG - Server package in client code
import { ... } from "better-auth"
// CORRECT - Client package for client code
import { createAuthClient } from "better-auth/client"
better-auth is the
main entry point for everythingWhen users import better-auth in client code:
node:sqlite end up in client bundlesPR #4360 added a typeof window === "undefined" check to prevent
node:sqlite errors.
This was a workaround, not the cause.
The real issue is users importing server packages in client code.
// In any client-side file (React, Vue, Solid, etc.)
// NEVER do this:
import { anything } from "better-auth"
// ALWAYS do this:
import { createAuthClient } from "better-auth/client"
import type { Session, User } from "better-auth/types"
While fixing test infrastructure, the real issue was revealed:
window checks does not
fix users importing the wrong packageAdd runtime detection:
// In better-auth/index.ts
if (typeof window !== "undefined") {
throw new Error(
"You are importing 'better-auth' in client code. " +
"Use 'better-auth/client' instead."
);
}
Update documentation - Add prominent warnings about imports
Fix auto-imports - Configure package.json to guide IDEs
Build-time validation - ESLint plugin to catch wrong imports
Better examples - Clearly separate server and client code
If you are seeing build errors with node:* modules:
better-auth in client codebetter-auth/clientThis is not a bug in Better Auth - it is incorrect usage.