Skip to content

createIsomorphicFn causes circular dependency when imported in route files #6297

@mikecfisher

Description

@mikecfisher

Which project does this relate to?

Start

Describe the bug

Related to #5673 which was fixed for Node.js in v1.142.1, but the issue persists on Bun runtime due to its stricter module initialization order.

When using createIsomorphicFn (or createServerOnlyFn/createClientOnlyFn) in a utility file that gets imported by route files, it causes a circular dependency error during dev server startup:

ReferenceError: Cannot access '__vite_ssr_import_2__' before initialization

The route tree imports a file that imports from @tanstack/react-start, which tries to access router internals before they're initialized.

The dependency chain:

router.tsx → routeTree.gen.ts → my-route.tsx → utils/logger.ts → @tanstack/react-start → router (not ready yet)

On Node.js post-v1.142.1, this is rare/intermittent. On Bun, it's consistent because Bun is stricter about module initialization order.

Your Example Website or App

I don't have a minimal repro yet but can create one if needed.

Steps to Reproduce the Bug or Issue

  1. Create a utility file that uses createIsomorphicFn from @tanstack/react-start
  2. Import and use that utility in a route file
  3. Start the dev server with Bun (bun --bun run dev)
  4. See ReferenceError: Cannot access '__vite_ssr_import_2__' before initialization

Expected behavior

I expected to be able to use createIsomorphicFn in any file without worrying about import order. The environment functions should be safe to import at module level in files that route files depend on.

Workaround

Dynamically import createIsomorphicFn so it only loads after the router is initialized:

let _fn = null;
async function init() {
  if (\!_fn) {
    const { createIsomorphicFn } = await import("@tanstack/react-start");
    _fn = createIsomorphicFn().client(...).server(...);
  }
  return _fn;
}

Platform

  • Start Version: 1.142.8
  • OS: macOS
  • Runtime: Bun 1.2.x (issue is consistent), Node.js (issue is rare post-v1.142.1)
  • Bundler: Vite 6.x

Additional context

The fix in #6160 (v1.142.1) resolved the issue for Node.js users, but Bun's stricter module initialization order still triggers the circular dependency consistently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions