this.BJSCANNON.Vec3 is not a constructor

This was the error:

This was the piece of code generating the error:
image

I followed a similar topic asked a while back and it said that you need to install cannon from npm and type window.CANNON = require('cannon'), however after I did this it said this:

After downloading and following the steps given it is giving me this error:

image

My tsconfig.json is:

image

Thank you for reading.

If you use imports you will need to import cannon (instead of requiring it). In any way you need to provide Babylon with a reference to cannon. Try “import * as CANNON from ‘cannon’;” instead of the require call

Mixing import and require doesnt matter with webpack, they both get transpiled to webpacks require. Its more like, what combinatric of config you land on after rolling the jsfucklandia 20d40 dice. The constructor error is probably from babel / webpack / ts mismatch on how to apply namespaces. Basically if babel touches the code before typescript, you may have to do require(‘cannon’).default or add esModuleInterop:true to your tsconfig , which just wraps everything to try both. Maybe need to add moduleResolution:’node’ and allowJs:true to your tsconfig also bc ts loader and webpack are both flawed with how they look for things, dont want to rage and elaborate any more than that.

From your error, require(‘cannon’) call does return something, not undefined. But it is not a closure that contains Vec3.

Can you try to do the following and see what is the output?

 const cannon = require('cannon');
 console.log(cannon);

I guess the output is a function like recast. And you need to instantiate it like this:

 const cannon = require('cannon');
 window.CANNON = cannon();

or

 window.CANNON = require('cannon')();

I have tried RaananW’s solution, however, it gives the same error. I have added the esModuleInterop and allowJs: true and the moduleResolution but it is the same. For Slin’s solution it says it not a function :frowning:
I think I should reinstall the packages and copy paste the code. Thank you all for your help though.

@Jerome_B If all else fails, I updated cannon using several variants here. .Sketchbook/src/sketchbook/physics at master · jeremy-coleman/Sketchbook · GitHub , feel free to copy/paste. I modified some prototypes (just to reimplement the original sketchbook author’s changes), but I dont think it should have any effect on babylon, because they are just additive changes. I have some alternate copies on my disk somewhere if those dont work for you, I have an unbundled esm single file version of just plain cannon 0.6.2 somewhere.

Sidenote, is it just me or are like 10% of babylon users named jeremy or jerome? thanks mom

@jeremy-coleman Thank you very much.

Sidenote: we are taking over.