Skip to content

[Bug]: RuntimeError: factory is undefined during HMR of lazy-loaded React component with a specific dependency. #11922

Closed

Description

@vitexikora

Version

System:
    OS: Windows 11 10.0.26100
    CPU: (4) x64 Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz
    Memory: 17.25 GB / 31.83 GB
  Browsers:
    Firefox: 144.0
    Chrome: 141.0.7390.108
    Edge: Chromium (140.0.3485.54)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @rsbuild/core: ^1.5.17 => 1.5.17
    @rsbuild/plugin-react: ^1.4.1 => 1.4.1
    @types/md5: ^2.3.6 => 2.3.6
    @types/node: ^24.8.1 => 24.8.1
    @types/react: ^19.2.2 => 19.2.2
    @types/react-dom: ^19.2.2 => 19.2.2
    @types/webpack-env: ^1.18.8 => 1.18.8
    es-toolkit: ^1.40.0 => 1.40.0
    md5: ^2.3.0 => 2.3.0
    react: ^19.2.0 => 19.2.0
    react-dom: ^19.2.0 => 19.2.0
    typescript: ^5.9.3 => 5.9.3

Details

When updating a React lazy-loaded component, that has a specific (even nested) dependency (eg. md5 or matrix-js-sdk among those I have tested), when this update includes a new import, it always ends up in a RuntimeError: factory is undefined.
Common updates (such as updating a text) do not trigger this.
If the component does not include the "problematic" dependency, or this dependency is within its own lazyload, the issue does not trigger as well.
Removing the top-level lazyload solves this as well.

In our use case, this problem started being very burdensome after working with matrix-js-sdk. The whole view containing usages of the SDK is very hard to develop, every new import ends up in an error & requires full reload. Even after moving the SDK to its separate lazy-load, the issue reappeared when importing enums from this lib.
After some tinkering, I have also found out that the md5 lib has the same issue. And there are definitely many more. You need to find the root cause.

I have tested rsbuild versions 1.4.10, 1.4.15 and 1.5.17 - the error is in all of them.

See the attached reproduction repo - clone & edit TestPage.tsx (see readme).

Thanks for looking into this!

Reproduce link

https://github.com/vitexikora/rspack-hmr-bug-repro

Reproduce Steps

  1. clone repo
  2. pnpm i
  3. pnpm start
  4. edit TestPage.tsx (uncomment the line with "isEqual", see comments)
  5. inspect console for errors, see that HMR failed

You can then refresh the page and try again. The HMR will fail every time.

Activity

  1. added theissue type on Oct 17, 2025
  2. added theissue type on Oct 20, 2025
  3. stormslowly commented on Oct 28, 2025

    @stormslowly
    Contributor

    Thank you for reporting this case.
    It's similar to #11324 (comment)
    You can try this config

      plugins: [
        pluginReact({
          reactRefreshOptions: {
            reloadOnRuntimeErrors: true,
          },
        }),
      ],

    But I have missed a case:

    1. Start with calling isEqual.
    2. Delete calling to isEqual.
    3. HMR works as expected, but replace module isEuqual.mjs's factory to warnUnexpectedRequire
    4. add back calling to isEqual
    5. The lazy module requires isEuqual.mjs before node modules' hmr chunk applied.
    6. The warnUnexpectedRequire called as a module factory, HMR failed in silence.

    I will fix it soon.

    If different chunk splitting between dev and prod is acceptable, disabling chunk splitting while development is also a workaround option.

      tools: {
        rspack: {
          optimization: {
            splitChunks: env === "development" ? false : undefined,
          },
        },
      },
  4. vitexikora commented on Oct 29, 2025

    @vitexikora
    Author

    Thanks for looking into this.
    I will temporarily disable chuck splitting on dev until it is resolved.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions