Import matter-js in typescript project
Asked Answered
D

5

8

I found this file :

https://www.npmjs.com/package/@types/matter-js

i execute this line :

npm install --save @types/matter-js

In root ts file i got error message :

'Matter' refers to a UMD global, but the current file is a module. Consider adding an import instead.

Code looks like :

///<reference path="matter-js.d.ts"/>

import Ioc from "./libs/ioc";
let master = new Ioc();
console.log(master);
console.log(Matter);

Error in browser:

app.ts:11 Uncaught ReferenceError: Matter is not defined

Did i need to load matter-js.js lib ?

If i put :

import matter from 'matter-js'

i get :

Module ''matter-js'' has no default export.

Desantis answered 9/4, 2018 at 13:53 Comment(0)
F
9

You can try to load it using import * as matter from 'matter-js', that should work.

Futures answered 19/5, 2018 at 9:15 Comment(1)
I removed this line ///<reference path="matter-js.d.ts"/> . No need at all now.Desantis
U
4

You can also do

var Matter = require('matter-js');

or

import { Engine, World, Body, Bodies, Constraint } from 'matter-js';

The @types/matter-js is missing the Common class, but maybe you shouldn't use that anyway. If you really need it, you can add this to the index.d.ts in the package,

export class Common {
    static extend(obj:any, deep?:boolean): any;
    static clone(obj:any, deep?:boolean): any;
    static keys(obj:any): any;
    static values(obj:any): any;
    static get(obj:any, path:string, begin:number, end:number): any;
    static set(obj:any, path:string, val:any, begin:number, end:number): any;
    static shuffle(array:any[]): any;
    static choose(choices:any[]): any;
    static isElement(obj:any):boolean;
    static isArray(obj:any):boolean;
    static isFunction(obj:any):boolean;
    static isPlainObject(obj:any):boolean;
    static isString(obj:any):boolean;
    static clamp(value:number, min:number, max:number):number;
    static sign(value:number): number;   
    static now(): number;
    static random(min?:number, max?:number): number;
    static colorToNumber(colorString:string): number;
    static log(...objs:any[]): void;
    static info(...objs:any[]): void;
    static warn(...objs:any[]): void;
}
Unionist answered 16/8, 2019 at 17:40 Comment(0)
P
3

Unfortunately, the typescript lib seems outdated. Last commit 2 years ago. Parameters that should be optional are instead required in the typescript definitions...

Pappano answered 19/5, 2018 at 9:5 Comment(0)
S
2

I load the matter.js library with a script tag. Did you forget that?

<script src="js/matter.min.js"></script>

And I installed the typings with

npm install @types/matter-js
Shayne answered 9/4, 2018 at 18:14 Comment(2)
It is correct but if i already use ts, webpack i wanted to get absolute all app files from src/ to build/ based on dependency...Desantis
Just go install the types npm i @types/matter-js -D.Gratification
K
0

I got errors for my matterjs import until I deleted my node_modules and ran npm install again. Then the problems went away. Ended up with the following:

import * as Matter from 'matter-js';
Ked answered 3/10, 2021 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.