Adding Physics to a Typescript project

I haven’t had any trouble working with cannon.js in the playground. I am trying to convert this playground example to a standalone typescript project with webpack.

For the life of me, I cannot get the syntax right for importing and setting up CannonJSPlugin(). I tried following an earlier post, but the solution there doesn’t seem to work for me. Or I’m doing something wrong, which is likely!

My current failing code is in this branch on GitHub. I keep running in to the error

Uncaught ReferenceError: CANNON is not defined
    at new CannonJSPlugin (cannonJSPlugin.js:23:82)
    at new Main (main.ts:22:30)
    at eval (main.ts:69:1)
    at ./src/main.ts (bundleName.js:105:1)
    at __webpack_require__ (bundleName.js:15628:33)
    at bundleName.js:16700:37
    at bundleName.js:16702:12

The exact line that is throwing the error is here.

VSCode is aware of CANNON and will do intellisense for it. And I compile without errors. I’m only running in to this on execution.

I don’t see you importing or including CANNON anywhere.
It isn’t included in BJS and needs to be imported or otherwise included

1 Like

Thanks, that clued me in to what I had done wrong.
import cannon from "cannon"; was giving me an error, so I had removed it. After I’d installed with npm install --save-dev @types/cannon, I had CANNON resolving in intellisense. I misunderstood the original article that I’d read and didn’t realize I also needed npm install --save-dev cannon.

type types can be --save-dev, but cannon I think should be a regular dependency unless you bundle another way?

I could very easily be doing something wrong. I’m new to this toolchain, having worked mostly in Unity and Unreal before. So far, this has worked with babylonjs done with --save-dev.

Yes - will work until you deploy. The types are “erased” at build time, so are ok as a dev dependency. If you look in package.json it will show dev vs. Regular dependencies. :smile:

When you publish a package to npm, the dev deps are not installed when a user installs your package. For apps, it doesnt actually matter, but the convention is to put packages that will be in the client bundle under dependencies and build tools under devDependencies.

1 Like