Stumped by a weird error trying to run DOMParser.fromSchema

Hi again!

I’m moving my PM database-converter script to Node.js and I’m stuck on a weird error I just can’t get my head around.

Here’s my simplified code:

const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { DOMParser } = require('prosemirror-model');
const pmSchema = require('prosemirror-schema-basic');

const { document } = (new JSDOM('<p>Foo</p>')).window;
const parsedHTML = DOMParser.fromSchema(pmSchema)
    .parse(document, { preserveWhitespace: true })

And here’s the error:

  return schema.cached.domParser ||

TypeError: Cannot read property 'domParser' of undefined
    at Function.fromSchema (/home/raphael/dev/sweet-api/node_modules/prosemirror-model/dist/index.js:2685:24)
    at Object.<anonymous> (/home/raphael/dev/sweet-api/workers/posts-to-prosemirror.js:15:26)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

The code in question:

DOMParser.fromSchema = function fromSchema (schema) {
  return schema.cached.domParser ||
    (schema.cached.domParser = new DOMParser(schema, DOMParser.schemaRules(schema)))

All I can think this is is a problem with JSDOM, because the same DOMParser.fromSchema call works fine in the browser.

1 Like

You’re passing the result of require("prosemirror-schema-basic") as if it is a Schema instance—but it isn’t, it’s an exports object. So do DOMParser.fromSchema(pmSchema.schema) instead.

Oh, duh. Thank you so much!