Im using Babylon in headless mode using NullEngine in Node.Js
BABYLON.InitializeCSG2Async() does not enjoy being outside of the browser.
Is there a workaround?
I’ve tried to load jsdom manually to no extend (initialize seems to enter an infinite loop now).
import { JSDOM } from ‘jsdom’;
const dom = new JSDOM(‘’);
globalThis.window = dom.window;
globalThis.document = dom.window.document;
It seems to me doing boolean operation should not depend on the browser/window/dom.
Well first what is the error? Init does not require access to webgl but csg2 will create and manipulate mesh so that will require a gl context for sure (unless you stick with VertexData then it should work)
That’s the error I got prior to doing the little hack mentionned in the original question.
Error: Cannot load script module outside of a window or a worker
at […]node_modules@babylonjs\core\Misc\tools.internals.js:31:20
at new Promise ()
at _LoadScriptModuleAsync ([…]node_modules@babylonjs\core\Misc\tools.internals.js:18:12)
at Module.InitializeCSG2Async ([…]node_modules@babylonjs\core\Meshes\csg2.js:344:30)
I’m relatively new to node.js but it seems to support wasm (but it might be accessed / behave differently). I’ve never toyed with webassembly yet.
Node.js does support WASM. The exception comes from our code and not from node directly. The code is this:
if (IsWindowObjectExist()) {
windowAsAny = window;
windowString = "window";
} else if (typeof self !== "undefined") {
windowAsAny = self;
windowString = "self";
} else {
reject(new Error("Cannot load script module outside of a window or a worker"));
return;
}
And yes, this will fail in node, as node doesn’t have windows nor self. It does support globalThis, so it should be working correctly with slight adjustments. I can run a few tests tomorrow and see if that solves the issue for js runners (node/deno/bun?)