How to import Ammo.js into react-babylonjs

681 views Asked by At

I'm trying to import Ammo physics engine into react-babylon project, build with CRA. I keep getting polyfill error:

Compiled with problems:X

ERROR in ./node_modules/ammo.js/builds/ammo.js 207:22-45

Module not found: Error: Can't resolve 'path' in 'C:\...\node_modules\ammo.js\builds'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }


ERROR in ./node_modules/ammo.js/builds/ammo.js 210:18-31

Module not found: Error: Can't resolve 'fs' in 'C:\...\node_modules\ammo.js\builds'

Component using import looks like this:

//@ts-ignore
import { default as Ammo } from 'ammo.js/builds/ammo';
Ammo();
...
<Scene enablePhysics={[gravity, new AmmoJSPlugin(false, Ammo())]}>

I assume the problem comes from WASM compilation but I have absolutely no idea how to tackle it. If possible I would prefer not to eject CRA.

1

There are 1 answers

0
sancelot On

you have to use @craco/craco to override webpack config