HubSpot/draft-convert

Encounter "ReferenceError: document is not defined" when call convertFromHTML() on Node.js server side

Opened this issue · 0 comments

Hi,

I am trying to use "draft-convert" package on Node.js servide side. When I call convertFromHTML(html) , I got the following error:


ReferenceError: document is not defined
at fallback (/node_modules/draft-convert/lib/util/parseHTML.js:9:13)
at parseHTML (/node_modules/draft-convert/lib/util/parseHTML.js:25:11)
at getChunkForHTML (/node_modules/draft-convert/lib/convertFromHTML.js:458:18)

I followed some online posts to pass DOMBuilder function as a second parameter to convertFromHTML() like the following:


// Define the DOMBuilder function
function DOMBuilder(html) {
const dom = new JSDOM(<!DOCTYPE html><body>${html}</body>);
return dom.window.document.body;
}

// Call convertFromHTML with the DOMBuilder function and required classes
const html = 'Hello Text';
const contentState = convertFromHTML(html, DOMBuilder);

However, I still got the same "document is not defined" error.

Does anyone have the same problem when using this package on Node.js? I am using the latest version of "draft-convert".