Code block "Cannot find name 'require'"

So I have an RSS feed that includes full HTML in the body, I want to process it and convert it to plain text before sending to my future steps…

I’ve added a code block and imported an module html-to-text via NPM (html-to-text - npm).

In my index.ts I have:

export const code = async (inputs) => {
    const { convert } = require('html-to-text');
    const { OutputText } = convert(inputs.InputHTML);
    return   OutputText ;
};

And things were working okay, but now “require” is giving a red underline saying that ‘require’ cannot be found and it has stopped working (“Cannot find name ‘require’. Do you need to install type definitions for node?”). I had been messing on trying to shorten the OutputText down to say 2048 chars in length and seem to have messed up what I had working!

Any pointers about how to get back up and running appreciated please (and bonus for showing me how to then trim / parse / substring the output! Thank you :slight_smile:

Hi @Bibbleq .You need to use the

import { something } from 'somewhere';

at the beginning of the code.

So maybe in your case:

import {convert } from 'html-to-text'

Example: How to extract transcript from Youtube video

Also, this is how you trim a string:

const somestring = "this is a string"; //(your output string variable)
const length = 7;
const trimmedString = somestring.substring(0, length);

1 Like

Thank you so much.

For any future searches this is what I ended up with that worked:

Index.ts:

import { convert } from 'html-to-text';
export const code = async (inputs) => {
    let OutputText  = convert(inputs.InputHTML.substring(0,2048), {wordwrap: null});
    return OutputText.substring(0,1024) ;
};

package.json:

{
  "dependencies": {
    "html-to-text": "9.0.5"
  }
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.