[GH-ISSUE #2310] useSyncExternalStore not recognized in react19 #9142

Closed
opened 2026-04-13 04:29:57 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @rittamdebnath on GitHub (Apr 16, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2310

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Terminal error upon bun run build

bun run build
$ next build
   ▲ Next.js 15.3.0
   - Environments: .env

   Creating an optimized production build ...
Failed to compile.

./node_modules/better-auth/dist/client/react/index.mjs
Attempted import error: 'useRef' is not exported from 'react' (imported as 'useRef').

Import trace for requested module:
./node_modules/better-auth/dist/client/react/index.mjs
./src/lib/auth-client.ts

./node_modules/better-auth/dist/client/react/index.mjs
Attempted import error: 'useSyncExternalStore' is not exported from 'react' (imported as 'useSyncExternalStore').

Import trace for requested module:
./node_modules/better-auth/dist/client/react/index.mjs
./src/lib/auth-client.ts


> Build failed because of webpack errors
error: script "build" exited with code 1

auth-client.ts

import { createAuthClient } from "better-auth/react";
import { customSessionClient } from "better-auth/client/plugins";

// Base URL fallback for better reliability
const baseURL = process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000";

if (!process.env.NEXT_PUBLIC_BETTER_AUTH_URL) {
  console.warn("Warning: NEXT_PUBLIC_BETTER_AUTH_URL is not defined. Using default localhost URL.");
}

// Dynamic plugin configuration (optional)
const plugins = [customSessionClient()];

// Create the auth client
export const authClient = createAuthClient({
  baseURL,
  plugins,
});

// Utility functions for common operations
export async function signIn(email: string, password: string) {
  return authClient.signIn.email({
    email,
    password,
    callbackURL: "/overview",
  });
}

export async function getSession() {
  const { data: session } = await authClient.getSession();
  if (!session) {
    throw new Error("No session found");
  }
  console.log("Session data:", session);
  return session;
}

Current vs. Expected behavior

Current behavior: build throws and error
Expected behavior: should complete build

What version of Better Auth are you using?

1.2.7

Provide environment information

- MAC 
- SAFARI

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

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @rittamdebnath on GitHub (Apr 16, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2310 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Terminal error upon `bun run build` ``` bun run build $ next build ▲ Next.js 15.3.0 - Environments: .env Creating an optimized production build ... Failed to compile. ./node_modules/better-auth/dist/client/react/index.mjs Attempted import error: 'useRef' is not exported from 'react' (imported as 'useRef'). Import trace for requested module: ./node_modules/better-auth/dist/client/react/index.mjs ./src/lib/auth-client.ts ./node_modules/better-auth/dist/client/react/index.mjs Attempted import error: 'useSyncExternalStore' is not exported from 'react' (imported as 'useSyncExternalStore'). Import trace for requested module: ./node_modules/better-auth/dist/client/react/index.mjs ./src/lib/auth-client.ts > Build failed because of webpack errors error: script "build" exited with code 1 ``` ## auth-client.ts ``` import { createAuthClient } from "better-auth/react"; import { customSessionClient } from "better-auth/client/plugins"; // Base URL fallback for better reliability const baseURL = process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"; if (!process.env.NEXT_PUBLIC_BETTER_AUTH_URL) { console.warn("Warning: NEXT_PUBLIC_BETTER_AUTH_URL is not defined. Using default localhost URL."); } // Dynamic plugin configuration (optional) const plugins = [customSessionClient()]; // Create the auth client export const authClient = createAuthClient({ baseURL, plugins, }); // Utility functions for common operations export async function signIn(email: string, password: string) { return authClient.signIn.email({ email, password, callbackURL: "/overview", }); } export async function getSession() { const { data: session } = await authClient.getSession(); if (!session) { throw new Error("No session found"); } console.log("Session data:", session); return session; } ``` ### Current vs. Expected behavior Current behavior: build throws and error Expected behavior: should complete build ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - MAC - SAFARI ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 04:29:58 -05:00
Author
Owner

@JaafarAlMuallim commented on GitHub (Apr 16, 2025):

I encountered the same build error with Next.js. Switching from const { data } = await authClient.getSession(...) to const sessionCookie = getSessionCookie(request); in my middleware resolved the issue.

<!-- gh-comment-id:2810928114 --> @JaafarAlMuallim commented on GitHub (Apr 16, 2025): I encountered the same build error with Next.js. Switching from `const { data } = await authClient.getSession(...)` to `const sessionCookie = getSessionCookie(request);` in my middleware resolved the issue.
Author
Owner

@Bekacru commented on GitHub (Apr 17, 2025):

Don't import react authClient in your middleware

<!-- gh-comment-id:2812147818 --> @Bekacru commented on GitHub (Apr 17, 2025): Don't import react `authClient` in your middleware
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9142