Skip to content

1.7.2 ESM bundle throws require("react") at runtime in webpack/rspack builds #79

Description

@khainguyen-ws

Summary

react-show-more-text@1.7.2 (published 2026-04-18) ships a rolldown-bundled ESM at dist/index.es.js that calls require("react") through an indirect helper webpack/rspack can't statically rewrite. At runtime the helper falls into its fallback branch and throws, crashing any page that renders <ShowMoreText>.

Likely the same underlying ESM-bundle bug as #78 (which is the Vite-side symptom: does not provide an export named 'default').

Error

Uncaught Error: Calling `require` for "react" in an environment that doesn't expose the `require` function.
See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.

Evidence

Top of the published dist/index.es.js:

import e,{Component as t}from"react";
import r,{PropTypes as n}from"prop-types";
var o=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),
    i=/* @__PURE__ */(e=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(e,{get:(e,t)=>("undefined"!=typeof require?require:e)[t]}):e)(function(e){
      if("undefined"!=typeof require)return require.apply(this,arguments);
      throw Error('Calling `require` for "'+e+"\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.")
    }),
    s=/* @__PURE__ */o(e=>{
      var t=i("react"),                              // <-- thrown here in browsers
          r=Symbol.for("react.element"),
          n=Symbol.for("react.fragment"),
          ...

The i("react") call (the indirect rolldown CJS shim) is what blows up. Webpack can't see it as a static require("react") and leaves it alone, so the helper's fallback throw is what executes.

Root cause

The rolldown build inlines a CJS copy of react/jsx-runtime into the ESM bundle and re-requires react from inside that inlined copy. react is marked external but react/jsx-runtime is not, so the bundler emits the runtime require shim instead of a real ESM import.

How to reproduce

  1. npm install react-show-more-text@1.7.2 react react-dom in any webpack 5 project (no special config — happens with stock CRA-style setups too).
  2. import ShowMoreText from 'react-show-more-text' and render anywhere.
  3. Production build, load the page. The chunk containing ShowMoreText throws the error above as soon as the module evaluates.

Suggested fixes (any one)

  • Drop the exports["."].import / module fields in package.json. Consumers fall back to dist/index.cjs.js, which is well-formed.
  • In the rolldown config, mark react/jsx-runtime as external alongside react (or both react/*), so the bundler emits a normal import { jsx } from 'react/jsx-runtime' instead of inlining + re-requiring.
  • Ship the ESM by re-emitting through a bundler whose CJS-interop path doesn't require a runtime require (e.g. tsup with --external react --external react/jsx-runtime).

Workaround for downstream users

Pin to 1.7.1 — it ships only the babel-compiled CJS at lib/ShowMoreText.js and works fine.

Happy to send a PR if helpful — just let me know which approach you'd prefer.

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