Using Recast.js via NPM package import

Hi!

You can “fix” this issue like this:

The webpack 5 issues can be fixed as well:

Have a look at the full topic here:

Hopefully this helps!

:vulcan_salute:

R.

Ok, I just given a try to use recast-detour and looks I’ve failed one more time.
Now I made build broken build and uploaded it to GitHub.
Can someone please take a look at this? What do I do wrong here?

Uncaught (in promise) TypeError: this.bjsRECAST.Vec3 is not a constructor

The minimal project build with its sources is here:

You need to make sure to initialize the recast detour package:

Yeah, I have it installed:

$ npm list | grep -i recast
├── recast-detour@1.6.2

But at this line:

import * as Recast from "recast-detour";
//...
const recast = new Recast(); // <--

I get this error:

This expression is not constructable.
  Type 'typeof import("/home/rata/projects/node_modules/recast-detour/recast")' has no construct signatures. (tsserver 2351)

Is this the right package? Maybe it’s too fresh? :slightly_smiling_face:

You have Recast installed but not initialized.
Please have a look how it is done here - babylonjs-webpack-es6/navigationMeshRecast.ts at master · RaananW/babylonjs-webpack-es6 · GitHub

Looks I’ve one more trouble with the typescript and recast-detour. My typescript gets the error when trying to compile recast-detour:

ERROR in /home/rata/projects/node_modules/recast-detour/recast.d.ts
115:36-37
[tsl] ERROR in /home/rata/projects/node_modules/recast-detour/recast.d.ts(115,37)
      TS1005: '(' expected.
ts-loader-default_e3b0c44298fc1c14

The last line of recast.d.ts that are the root of the problem look like this:

export default () => Promise<Recast>;
$ tsc --v                                                                                                                                 
Version 4.8.2

Hey!

This setup always worked for all my TS based projects:

package.json

    "recast-detour": "^1.5.0",

class

import 'recast-detour'
import 'RecastJSPlugin' from '@babylonjs/core'
import Recast from 'src/recast/recast'
...
private _navigationPlugin?: RecastJSPlugin
...
private async _setupCrowd() {
  const recast = await Recast()
  const navigationPlugin = new RecastJSPlugin(recast)
  this._navigationPlugin = navigationPlugin
  ...
}

Download https://babylonjs.xyz/recast.js and put it into src/recast:

And modify the file recast.d.ts as seen here:

:vulcan_salute:

2 Likes

That worked for me

1 Like