Skip to content

Dev server SSR do not load ESM version of externalized package #6125

Closed

Description

@guyutongxue

Which project does this relate to?

Start

Describe the bug

If a package has following package.json and no exports is defined:

{
    "name": "my-lib",
    "main": "src/index.cjs",
    "module": "src/index.mjs"
}

Then if it was externalized in vite.config.ts:

// vite.config.ts
const config = defineConfig({
  ssr: {
    external: true,
  },
  // rest...
});

Then when launch the dev server with vite, the SSR resolution will pick the CJS version of my-lib from main field, although we are importing it.

Your Example Website or App

https://github.com/guyutongxue/vite-ssr-cjs-issue

Steps to Reproduce the Bug or Issue

  1. bun i and cd packages/my-app and bun dev
  2. Launch the App, and you will see the my-lib was imported as CJS and hydrated later to ESM.

The my-lib just export a simple function returning which module type it is; and the app code just import it and render the return value to <App>.

Expected behavior

Expected always ESM with no hydration mistmatch.

Screenshots or Videos

Image

Platform

  • Router / Start Version: 1.141.5
  • OS: Windows, also reproduced on Mac
  • Browser: Edge
  • Browser Version: 143.0.3650.80
  • Bundler: Vite
  • Bundler Version: 7.3.0

Additional context

I'm not sure whether this is a Vite bug because I cannot make a reproducible example code on pure Vite SSR (too low level sorry).

I figured out that TanStack Start run into the code in Vite here:

https://github.com/vitejs/vite/blob/ef3d59648fd9dd3f9b3118d09d216dc0afcb8c33/packages/vite/src/node/ssr/fetchModule.ts#L52

that won't consider ESM module at all.

Activity

  1. coderabbitai commented on Dec 17, 2025

    @coderabbitai
    Contributor

    📝 CodeRabbit Plan Mode

    Generate an implementation plan and prompts that you can use with your favorite coding agent.

    • Create Plan
    Examples
    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    🔗 Related PRs

    #5336 - test(e2e-Utils): export built package from e2eUtils [merged]
    #5557 - test(solid-start): serialization-adapters suite [merged]
    #5816 - docs(solid-start): start-bun example [merged]
    #5845 - docs(solid-start): i18n example [merged]
    #5847 - docs(solid-start): start-large example [merged]

    👤 Suggested Assignees


    🧪 Issue enrichment is currently in open beta.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord or schedule a call!

  2. schiller-manuel commented on Dec 17, 2025

    @schiller-manuel
    Collaborator

    i created a plain vite SSR example, and it also has the same issue: guyutongxue/vite-ssr-cjs-issue#1

    closing this here, you should take this upstream to vite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions