# Using RecastJS on NodeJS or Browser in ESM or CJS

**URL:** <https://forum.babylonjs.com/t/using-recastjs-on-nodejs-or-browser-in-esm-or-cjs/58709>\
**Category:** Tutorials and tips\
**Tags:** recast\
**Created:** [May 28, 2025, 9:36pm UTC](https://forum.babylonjs.com/t/using-recastjs-on-nodejs-or-browser-in-esm-or-cjs/58709 "2025-05-28T21:36:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ylacaute](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ylacaute/32/31802_2.png) [@ylacaute](https://forum.babylonjs.com/u/ylacaute)\
**Post date:** [May 28, 2025, 9:36pm UTC](https://forum.babylonjs.com/t/using-recastjs-on-nodejs-or-browser-in-esm-or-cjs/58709/1 "2025-05-28T21:36:29Z")

</div>

Hello !

I (once again) lost a few hours trying to understand why RecastJS wouldn’t load in an Angular app (v19).  
I got the following error message:

```auto
Cannot set properties of undefined (setting 'Recast')

```

I finally found the solution and wanted to share it 😊  
To use Recast anywhere, you need to **patch it**.

The usage is always the same (ESM build):

```typescript
import {RecastJSPlugin} from "@babylonjs/core";
import Recast from 'recast-detour';
...
const plugin: RecastJSPlugin = new RecastJSPlugin(await Recast());

```

But we need to patch 2 files :

- recast.js
- recast.es6.js

And replace `this["Recast"] = Module;` with `globalThis["Recast"] = Module;`

Until the library is updated, you can use [patch-package](https://www.npmjs.com/package/patch-package), just modify the files, create a patch, and apply it using a `postinstall` script.

Hope this helps!  
Y.

---

<div class="post-metadata">

**Author:** ![barisozcetin](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/barisozcetin/32/45452_2.png) [@barisozcetin](https://forum.babylonjs.com/u/barisozcetin)\
**Post date:** [May 28, 2025, 9:42pm UTC](https://forum.babylonjs.com/t/using-recastjs-on-nodejs-or-browser-in-esm-or-cjs/58709/2 "2025-05-28T21:42:38Z")

</div>

alternatively, if you’re using vite, adding a plugin like this should do it

```auto
{
        name: "fix-recast",
        transform(code, id) {
          if (id.includes("recast-detour.js") || id.includes("recast.es6.js")) {
            return code.replace(`this["Recast"]`, 'window["Recast"]'); // or globalThis["Recast"]
          }
        },
      },

```
