Closed thonkinator closed 3 months ago
It looks like the issue you're encountering might be related to the way paper
is being imported and initialized alongside @happy-dom/global-registrator
. The error indicates that paper.js is trying to access the document object before it's fully registered by happy-dom. To address this, you could try dynamically importing paper after happy-dom has registered the global document. Here's how you could adjust your code:
import { GlobalRegistrator } from "@happy-dom/global-registrator";
GlobalRegistrator.register();
const paper = await import("paper");
paper.setup([36, 36]);
const circle = new paper.Shape.Circle([18, 18], 18);
paper.project.activeLayer.importSVG(`... svg data here ...`);
that does seem to make document
defined for the library, but the load fails anyway because apparently the library relies on getting a canvas context, which is unsupported by happy-dom. however, paper.js supports working without a canvas (like in a worker context) which it does by checking for window
, so manually defining window
as null makes it work. thank you!
import { GlobalRegistrator } from "@happy-dom/global-registrator";
GlobalRegistrator.register();
Object.define(global, "window", {
value: undefined,
});
const paper = await import("paper");
paper.setup([36, 36]);
const circle = new paper.Shape.Circle([18, 18], 18);
paper.project.activeLayer.importSVG(`... svg data here ...`);
Thank you for reporting @thonkinator and thank you for helping out @odanado! :slightly_smiling_face:
I'm importing the
paper
npm package, and I'm trying to usehappy-dom
because paper.js seems to rely on some browser APIs for some features. However, I'm getting errors aboutdocument
being undefined, like this:Gives this error:
Is
document
being undefined a bug withhappy-dom
, or is it just a limitation of the global registrator? Either way, what can I do to get this working?