Type="module" causes "Uncaught (in promise) engine should not be null"

Hello,
type=“module” causes “Uncaught (in promise) engine should not be null.”

Any ideas why?

Thanks for the responses in advance.

I think you will need to post some code and information showing what you are trying to do. Are you using webpack? etc. Obviously you are trying to use JS modules, but that’s not enough information to help with your problem because there are too many possibilities.

2 Likes

All what @kaliatech said, Thanks a lot :slight_smile:

cc @RaananW

1 Like

A basic demo. No webpack etc. just HTML and JS. It is just the Playground default Scene, but in an own js file.

I want to sort my code and put it in different files. And for that i want to use modules.
Maybe this is not possible in Babylon.js ?

If i use type=“module” it causes “Uncaught (in promise) engine should not be null.”

demo.zip (1.5 KB)

I think your issue is due to JS coding bugs. When script type=module is used, the this scope becomes undefined and it is no longer the window. Your code is setting engine/scene on window explicitly in some places, but using the current default scope in others. When not using type=module, they are the same global scope (window) and so it works.

If you were to change these lines in your code:

window.engine = await asyncEngineCreation();
...
window.scene = createScene();

to these instead:

engine = await asyncEngineCreation();
window.engine = engine;
...
scene = createScene();
window.scene = scene

Then things work. Your setup here is quite complex with the nested async functions, but I assume you have reasons.

2 Likes

It is the default Playground Code, when you downlaod the Scene

ah, I did not realize that. :laughing: I guess is related to how the playground wrapper works.

I’m glad you solved your problem with the module type script. JS modules… so cool, but so confusing.

An other Solution is this:

works without modules.