Trying to load vtk.js dynamically

I’m stuck on the following problem: I want to load vtk.js dynamically but I cannot get it to work (it does work sometimes - which makes the issue rather mysterious for me). Here is what I’m trying:

%%javascript
new Promise(function(resolve, reject) {
	var script = document.createElement("script");
	script.onload = resolve;
	script.onerror = reject;
	script.src = "https://unpkg.com/vtk.js@14.16.4/dist/vtk.js";
	document.head.appendChild(script);
}).then(() => {
console.log(vtk)
});

The code obviously works in jsfiddle. I already asked on the vtk.js forum (Loading vtk.js dynamically - Web - VTK). Is there something obvious I’m missing here?

BTW: If anyone is interested, I’m trying to implement rendering of cadquery models using vtk.js in jupyter notebooks:

This seems to work. I’d really would like to understand why…

new Promise(function(resolve, reject) {
        require.config({
        "paths": {"vtk": "https://unpkg.com/vtk"},
    }
);
    require(["vtk"], resolve);
}).then(() => {
    console.log(vtk);
    console.log("Loaded vtk.js library");
});