html-react-parser icon indicating copy to clipboard operation
html-react-parser copied to clipboard

Getting an error saying f is not a constructor

Open PenghaiZhang opened this issue 9 months ago • 11 comments

Expected Behavior

We have recently upgraded our build tool Parcel to v2.13.3, and since then we started having an error about f is not a constructor.

Image

This is how we use the parser:

  1. Import the parser through import HTMLReactParser from "html-react-parser";
  2. Pass a string to the parser

Image

It's expected no errors should occur.

We are using 3.0.16, but the error still occurs after we upgrade to 5.2.2. However, the error message is a bit more readable after the upgrade - Text is not a constructor.

Actual Behavior

Getting the error as described above.

Steps to Reproduce

Not sure if the issue is related to Parcel 2.13.x, but this is the only thing we did to trigger the issue.

Environment

  • Version: both 3.0.16 and 5.2.2
  • Platform:
  • Browser: Chrome 131.0.6778.204
  • OS: Ubuntu 22

PenghaiZhang avatar Mar 12 '25 04:03 PenghaiZhang

This may be related to the Text export from domhandler.

Can you try the following import?

import { Text } from 'html-react-parser';

console.log(Text);

remarkablemark avatar Mar 12 '25 04:03 remarkablemark

Hi @remarkablemark ,

Thanks for responding so quickly.

The output is undefined.

Image

PenghaiZhang avatar Mar 12 '25 05:03 PenghaiZhang

I found that using 'parcel watch' which does not minify and optimize the files, this error does not happen.

Image

PenghaiZhang avatar Mar 12 '25 05:03 PenghaiZhang

I wonder if this could be a parcel issue. Could you try nuking your node_modules and lockfiles and reinstalling dependencies?

rm -rf node_modules package-lock.json yarn.lock
npm i

remarkablemark avatar Mar 12 '25 16:03 remarkablemark

I wonder if this could be a parcel issue. Could you try nuking your node_modules and lockfiles and reinstalling dependencies?

rm -rf node_modules package-lock.json yarn.lock npm i

Hi @remarkablemark we have tried to clean up the node modules and re-install all the dependencies multiple times but this did not help fix the issue.

PenghaiZhang avatar Mar 14 '25 03:03 PenghaiZhang

Gotcha then I'll need your help in creating a small reproducible example and sharing the code with me. Feel free to use something like StackBlitz.

remarkablemark avatar Mar 14 '25 04:03 remarkablemark

Gotcha then I'll need your help in creating a small reproducible example and sharing the code with me. Feel free to use something like StackBlitz.

Thanks @remarkablemark ! I have tried to update that project but it seems there is a known issue in stackblitz: https://github.com/stackblitz/core/issues/1855

Basiically, I just replaced vite with Parcel 2 and the npm scripts and kept other code.

PenghaiZhang avatar Mar 14 '25 06:03 PenghaiZhang

I created a new small project locally and I found this error log.

@parcel/transformer-js: Unknown usage of CommonJS `exports` object. This causes tree shaking to be disabled.

  /home/penghai/parcel_test/test/node_modules/html-react-parser/lib/index.js:7:51
    6 | exports.htmlToDOM = exports.domToReact = exports.attributesToProps = exports.Text = exports.ProcessingInstruction = exports.Element = exports.Comment = void 0;
  > 7 | exports.default = HTMLReactParser;
  >   |                                   ^
    8 | var html_dom_parser_1 = __importDefault(require("html-dom-parser"));
    9 | exports.htmlToDOM = html_dom_parser_1.default;

  📝 Learn more: https://parceljs.org/features/scope-hoisting/#commonjs


@parcel/transformer-js: Unknown usage of CommonJS `exports` object. This causes tree shaking to be disabled.

  /home/penghai/parcel_test/test/node_modules/html-react-parser/lib/index.js:7:28
    6 | exports.htmlToDOM = exports.domToReact = exports.attributesToProps = exports.Text = exports.ProcessingInstruction = exports.Element = exports.Comment = void 0;
  > 7 | exports.default = HTMLReactParser;

PenghaiZhang avatar Mar 14 '25 07:03 PenghaiZhang

Once you create the project with your error, please commit it and share the public GitHub repo with me so I can take a look.

remarkablemark avatar Mar 14 '25 14:03 remarkablemark

Hi, thanks so much for your work on this package.

I'm experiencing the same issue on a project using esbuild, and it's only an issue when I build for prod and minify.

Let me know if it would help to provide a minimal code example in this same open issue.

njbair avatar May 26 '25 02:05 njbair

@njbair if you can provide a reproducible example, I can take a look

remarkablemark avatar May 26 '25 02:05 remarkablemark