# Cannot set properties of undefined Ammo

**URL:** <https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535>\
**Category:** Bugs\
**Tags:** documentation, tutorial, ammojs, physics\
**Created:** [August 30, 2022, 8:23pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535 "2022-08-30T20:23:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Firdeus\_Kasaj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/firdeus_kasaj/32/23441_2.png) [@Firdeus\_Kasaj](https://forum.babylonjs.com/u/Firdeus_Kasaj)\
**Post date:** [August 30, 2022, 8:23pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/1 "2022-08-30T20:23:37Z")

</div>

Hello everyone! So im having a bug where i installed the AmmoJS but when i use it its not working i dont really know why. I remember solving it once in another project but its the same code but now working😥. If you guys could help me a little bit.

The code:

```auto
import "@babylonjs/core/Debug/debugLayer";
import "@babylonjs/inspector";
import "@babylonjs/loaders/glTF";
import * as BABYLON from "@babylonjs/core";
import Ammo from "ammo.js";

import { ToonShaderComponent } from "./Shader/ToonShaderComponent";
import { MeshFollow } from "./MeshFollow";

//DOM Imports
import {handleFinishLoading} from "../view/loading"

class App {

  private sphere;
  private sphere1;

  constructor() {
    var canvas = document.getElementById("canvas");

    var engine = new BABYLON.Engine((canvas as HTMLCanvasElement), true);
    var scene = new BABYLON.Scene(engine);
    
    if(scene != null || scene != undefined){
      handleFinishLoading();
    }
    
    scene.clearColor = new BABYLON.Color4(0,0,0,0);
    
    this.CreatePhysics(scene, camera, engine);

    var camera: BABYLON.ArcRotateCamera = new BABYLON.ArcRotateCamera("Camera", 7.739712246449233, 0.01, 18.629691146092306, BABYLON.Vector3.Zero(), scene);
    camera.detachControl();
    camera.position = new BABYLON.Vector3(0.021241382121505482, 18.628759669297345, 0.18507886436930487);

    var light1: BABYLON.HemisphericLight = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);

    // this.CreateMeshes(scene, camera, engine);
    
    // camera.setTarget(sphere);

    window.addEventListener("keydown", (ev) => {
        // Shift+Ctrl+Alt+I to show debug layer
        if (ev.shiftKey && ev.ctrlKey && ev.altKey && ev.keyCode === 73) {
            if (scene.debugLayer.isVisible()) {
                scene.debugLayer.hide();
            } else {
                scene.debugLayer.show();
            }
        }

        if(ev.key == "e"){
          console.log("Camera Alpha: ", camera.alpha);
          console.log("Camera Beta: ", camera.beta);
          console.log("Camera Radius: ", camera.radius);
          console.log("Camera position: ", camera.position);
        }
    });

    engine.runRenderLoop(() => {
        scene.render();
        engine.resize();
    });
  }

  async CreatePhysics(scene, camera, engine): Promise<void> {
    const ammo = await Ammo();
    const physics = new BABYLON.AmmoJSPlugin(true,ammo);
    scene.enablePhysics(new BABYLON.Vector3(0,-100,0),physics);
    // this.CreateMeshes(scene, camera, engine);
    var cube = BABYLON.MeshBuilder.CreateBox("cube",{size:10},scene);
    cube.physicsImpostor = new BABYLON.PhysicsImpostor(cube,PhysicsImpostor.BoxImpostor,{mass:10,friction:1});
    cube.position = new BABYLON.Vector3(3,10,0);
  }

  CreateMeshes(scene, camera, engine){
    this.sphere = BABYLON.MeshBuilder.CreateSphere("name");
    var toonShaderComponent = new ToonShaderComponent(camera, scene, engine,this.sphere, new BABYLON.Color3(0.1,0.4,1));
    var meshFollow = new MeshFollow(scene, camera, this.sphere);

    this.sphere1 = BABYLON.MeshBuilder.CreateSphere("name");
    var toonShaderComponent1 = new ToonShaderComponent(camera, scene, engine, this.sphere1, BABYLON.Color3.Red());
    var meshFollow1 = new MeshFollow(scene, camera, this.sphere1);

    this.sphere.physicsImpostor = new BABYLON.PhysicsImpostor(this.sphere, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 1});
    this.sphere.physicsImpostor.physicsBody.linearDamping = 0;
    this.sphere.physicsImpostor.physicsBody.angularDamping = 0;

    this.sphere1.physicsImpostor = new BABYLON.PhysicsImpostor(this.sphere1, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 0});
    this.sphere1.physicsImpostor.physicsBody.linearDamping = 0;
    this.sphere1.physicsImpostor.physicsBody.angularDamping = 0;
  }
}
new App();

```

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/b/1bfea5f045609ce3bb15e070f91f3ee493b869e0.png)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [August 30, 2022, 8:40pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/2 "2022-08-30T20:40:04Z")

</div>

Probably you need to import ammo with types - [Babylon.js ES6 support with Tree Shaking | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/developWithBjs/treeShaking#ammo-with-types-enabled)

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 29, 2022, 12:33pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/3 "2022-09-29T12:33:57Z")

</div>

Hello @Firdeus_Kasaj just checking in if your issue was resolved?

---

<div class="post-metadata">

**Author:** ![Rommon\_Susiia](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rommon_susiia/32/20148_2.png) [@Rommon\_Susiia](https://forum.babylonjs.com/u/Rommon_Susiia)\
**Post date:** [October 21, 2022, 2:32am UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/4 "2022-10-21T02:32:52Z")

</div>

i imported ammojs-typed package and use it like example u provide,but is not work from the beginning,  
I am very confused,can u help me?  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/e/ae354f4a0ae62fd8fe3ed5bb3d38f88dac75e213.png)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/0/e0f4f302a3cfb1ce62cb1656761496651958e4e9.png)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/7/479d64ef0200445c2b8787bace6c1c033f9ab4e2.png)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [October 21, 2022, 4:52am UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/5 "2022-10-21T04:52:51Z")

</div>

Could you check how it is done here - [GitHub - RaananW/babylonjs-webpack-es6: Babylon.js basic scene with typescript, webpack, es6 modules, editorconfig, eslint, hot loading and more. Will even make coffee if you ask nicely.](https://github.com/RaananW/babylonjs-webpack-es6)  
To load the ammo physics demo, use `http://localhost:8080/?scene=physicsWithAmmo`

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [October 21, 2022, 5:33am UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/6 "2022-10-21T05:33:04Z")

</div>

Hey @Rommon_Susiia,  
Can you share more code or your project? It is difficult to say what is wrong with your code from such a small portion of the code.  
You can also check my very basic repo of BabylonJS + AmmoJS

> <https://github.com/neu5/babylonjs-ammojs-typescript-example/blob/master/src/main.ts>

---

<div class="post-metadata">

**Author:** ![Nawar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nawar/32/14923_2.png) [@Nawar](https://forum.babylonjs.com/u/Nawar)\
**Post date:** [February 21, 2023, 4:54pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/7 "2023-02-21T16:54:55Z")

</div>

```auto
import ammo from "ammo.js"
const Ammo = await ammo.bind(window)()

```

> <https://github.com/kripken/ammo.js/issues/374#issuecomment-1242809026>
>
> Hi there, for the last couple of days I've been struggling with getting this js …package to work in a bundled fashion. Both with vue-cli (webpack) and vitejs (rollup) I'm having issues getting it to work properly either at build or runtime.
> 
> With webpack for example I'm getting:
> 
> \`\`\`
> TypeError: Cannot set property 'Ammo' of undefined
> at ammo.js:978
> at h.load (diceThrower.js:29)
> \`\`\`
> 
> My own code (diceThrower.js:29) is simply: \`const ammo = await Ammo();\`
> 
> With the ammo.js error being at the end of the file
> 
> \`\`\`
> (function() {
> function a() {
> b.BT\_CONSTRAINT\_ERP = GA();
> b.BT\_CONSTRAINT\_STOP\_ERP = HA();
> b.BT\_CONSTRAINT\_CFM = IA();
> b.BT\_CONSTRAINT\_STOP\_CFM = JA();
> b.PHY\_FLOAT = KA();
> b.PHY\_DOUBLE = LA();
> b.PHY\_INTEGER = MA();
> b.PHY\_SHORT = NA();
> b.PHY\_FIXEDPOINT88 = OA();
> b.PHY\_UCHAR = PA()
> }
> Ta ? a() : Ra.unshift(a)
> }
> )();
> this.Ammo = b; // \<-- this line
> 
> return Ammo.ready
> }
> );
> }
> )();
> if (typeof exports === 'object' && typeof module === 'object')
> module.exports = Ammo;
> else if (typeof define === 'function' && define\['amd'\])
> define(\[\], function() {
> return Ammo;
> });
> else if (typeof exports === 'object')
> exports\["Ammo"\] = Ammo;
> \`\`\`
> 
> 
> Is there anyone that has a working example with webpack/rollup or some insights into what I might be doing wrong :(

---

<div class="post-metadata">

**Author:** ![Hongpeng\_Zhen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hongpeng_zhen/32/36508_2.png) [@Hongpeng\_Zhen](https://forum.babylonjs.com/u/Hongpeng_Zhen)\
**Post date:** [December 9, 2023, 5:43am UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/8 "2023-12-09T05:43:57Z")

</div>

hey guys, try this:

```auto
const ammo = await new Ammo()

```

---

<div class="post-metadata">

**Author:** ![nonesl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nonesl/32/42124_2.png) [@nonesl](https://forum.babylonjs.com/u/nonesl)\
**Post date:** [August 20, 2024, 8:44pm UTC](https://forum.babylonjs.com/t/cannot-set-properties-of-undefined-ammo/33535/9 "2024-08-20T20:44:36Z")

</div>

it works!
