node-html-to-text icon indicating copy to clipboard operation
node-html-to-text copied to clipboard

TypeError: selderee.DecisionTree is not a constructor.

Open trevlar opened this issue 3 years ago • 11 comments

Minimal HTML example

<div>Hello</div><p>World</p>

Options

import { convert } from '
convert("<div>Hello</div><p>World</p>");

Observed output

html-to-text.js:158 Uncaught TypeError: selderee.DecisionTree is not a constructor
    at compile (html-to-text.js:158:1)
    at convert (html-to-text.js:229:1)

Expected output

Hello\nWorld

Version information

  • html-to-text: 8.x
  • node: v14.18.0

I attempted to use 8.x, and 7.x with the same result.

I also used this other format that works in html-to-text 7.x though it has the same error above in html-to-text 8.x.

  import { htmlToText } from 'html-to-text';
  const text = htmlToText(
    '<div>Nope Its not Ashton Kutcher. It is Kevin Malone. <p>Equally Smart and equally handsome</p></div>',
    {
      wordwrap: 130,
    },
  );
  console.log(text);

trevlar avatar Apr 12 '22 19:04 trevlar

Are you using it in a web project? What kind and version of building/packaging tool is used?

KillyMXI avatar Apr 12 '22 20:04 KillyMXI

Are you using it in a web project? What kind and version of building/packaging tool is used?

I'm using it in a web project using create-react-app and TypeScript. "react": "^17.0.2", "react-scripts": "5.0.0", "typescript": "^4.5.4",

trevlar avatar Apr 12 '22 20:04 trevlar

This is likely to be related to #230 create-react-app relies on webpack and it is known to cause issues.

KillyMXI avatar Apr 12 '22 20:04 KillyMXI

Version 9 may also fix this issue when it's done, just because upstream packages will be more similar, less confusing for packaging tools.

KillyMXI avatar Apr 12 '22 20:04 KillyMXI

is there any quick fix for this?

selva-tuva avatar Jun 09 '22 08:06 selva-tuva

See the #230 thread. Webpack 4 needs some configuration.

KillyMXI avatar Jun 09 '22 13:06 KillyMXI

Most likely it's due to missing cjs loader. If you're using ejected cra, it's pretty straightforward. Just add a new rule to process cjs files. Here's an example if you're using cra5:

 {
      test: /\.(cjs)$/,
      exclude: /@babel(?:\/|\\{1,2})runtime/,
      loader: require.resolve('babel-loader'),
      options: {
        babelrc: false,
        configFile: false,
        compact: false,
        presets: [
          [require.resolve('babel-preset-react-app/dependencies'), { helpers: true }],
        ],
        cacheDirectory: true,
        cacheCompression: false,
        sourceMaps: shouldUseSourceMap,
        inputSourceMap: shouldUseSourceMap,
      },
},

If your cra is not ejected, you can try package like craco to customized cra webpack config. The idea is the same. Add a rule so that webpack recognize cjs modules.

Ideally this should be handled from html-to-text package side. It should load .mjs file.

neirongkuifa avatar Aug 07 '22 22:08 neirongkuifa

@neirongkuifa thank you.

What do you suggest to do inside html-to-text package? The package knows nothing about webpack and react. I don't know about these much either to fully understand your comment. I'd really prefer to avoid adding any dependency with "react" in the name.

(With the next major version and bumped dependencies there is going to be more consistency in the upstream packages. But looking at this error with the immediate dependency - I'm not sure webpack 4 will be able to figure it out better after the update, as long as html-to-text itself is an old-style package.)

KillyMXI avatar Aug 07 '22 22:08 KillyMXI

@KillyMXI This seems to be a promising solution. https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html.

The idea is instead of using .mjs, .cjs, rely on dynamic package json type to determine how to handle .js file. By doing this, you can avoid using .mjs and .cjs. Oh btw, this issue is actually from selderee package. Are you the maintainer for selderee as well?

neirongkuifa avatar Aug 07 '22 23:08 neirongkuifa

I believe even you're not the maintainer of selderee, you can still use some packing tool to compile html-to-text to js bundle and use the above solution in html-to-text package.

neirongkuifa avatar Aug 08 '22 00:08 neirongkuifa

I am the maintainer of selderee. But I would like to avoid maintaining a bundle for frontenders either way.

When making selderee and other packages I was considering what file extensions to use. I can't remember the exact reasoning why I settled on the current way. All options seem to have their pros and cons and something downstream can break either way.

When I'll switch html-to-text package to TypeScript, dual mode will be a natural outcome. But the package is not quite ready yet for the transition I think. And I don't want to involve tsc before I'm comfortable to go all the way.

Perhaps I can try to configure rollup (which I'm using already for v9) to produce both CommonJS and Module outputs for html-to-text. I made an attempt last year but it didn't work out for some reason so I stashed it. I'll try again.

KillyMXI avatar Aug 08 '22 09:08 KillyMXI

I published v9 preview version: npm i html-to-text@preview

I'd like to know whether the webpack compatibility issue still persists there.

KillyMXI avatar Nov 02 '22 17:11 KillyMXI

I tested the preview version and seems it's been fixed. It's been verified with CRA. @KillyMXI when will the v9 release be available?

reza-moghaddas-github avatar Nov 04 '22 14:11 reza-moghaddas-github

I don't know yet. 2022 sucks. I'm in another country, barely able to pay the rent, and can't make a buffer to free up a couple of weeks for an important feature. Looks like I will have to change my plans and leave some parts for later just to release something in a reasonable time, but there are still things to do either way. I will be able to look at it again at the end of this month, I think.

KillyMXI avatar Nov 04 '22 17:11 KillyMXI

Do you think we could have a temp fix for this issue meanwhile? Any config we could add in cra-override to add the rule that can fix it.

reza-moghaddas-github avatar Nov 04 '22 17:11 reza-moghaddas-github

The issue:

  • webpack 4 is a bit dumb and certain react projects seem to be stuck with that version;
  • html-to-text v8 is a cjs package that depends on some mixed-mode packages.

Possible solutions:

  • read #230, there is a comment that sounds close to what you're asking for;
  • or use html-to-text@preview for now if nothing else works;
  • I also released [email protected] not so far ago, in case that works and has enough features.

KillyMXI avatar Nov 04 '22 17:11 KillyMXI

@KillyMXI Didn't work for me. I pushed the new rule into rules in cra-override config file. But still seeing the exception raising in my page. I already upgraded to 8.2.* to have some fixes regarding that. Can't downgrade it to 7.*. Even end of this month would work for me if we could have the release by that time. I appreciate your time dude! Let me know if I can help on anything!

reza-moghaddas-github avatar Nov 07 '22 18:11 reza-moghaddas-github

@rmoghaddas-sedna Have you updated your start script? It won't work if you don't update the start script in package json. The issue is due to missing cjs file loader in cra5. Adding the loader should fix the issue, unless your override is not applied.

neirongkuifa avatar Nov 15 '22 11:11 neirongkuifa

I tried to upgrade to get the bugfix for this issue: https://github.com/html-to-text/node-html-to-text/issues/232 However I got the error mentioned in this issue. A simple hack for anyone else in my place:

  //Known issue with html-to-text package: preserveNewlines flag results in doubling of new lines.
  //Can't upgrade to latest version as it is crashing (known issue: https://github.com/html-to-text/node-html-to-text/issues/252).
  //Below hack will be used instead.

  htmlToTextResult = htmlToTextResult.replaceAll('\n\n', '\n');

PS. Thank you for your hard work @KillyMXI , I understand your situation

goamn avatar Nov 21 '22 12:11 goamn

@goamn could you please clarify which versions you tried to use? (I'm worried whether this issue still can happen with v9 preview)

KillyMXI avatar Nov 21 '22 12:11 KillyMXI

@KillyMXI I believe it was the latest version 8.2.1. I did not try v9 preview

goamn avatar Nov 22 '22 05:11 goamn

Version 9 is now public. Based on little feedback I've got here, I assume webpack issue should be fixed now. Please open a new issue in case there is any problem with version 9.

KillyMXI avatar Dec 03 '22 12:12 KillyMXI