Skip to content

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

@hzhang008

Description

@hzhang008

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".

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