matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Possibility of ES6 module support?

Open Rosuav opened this issue 8 months ago • 4 comments

Rather than a script element in the HTML, it would be helpful to be able to import as a module. For example:

import {Engine, Render, Bodies, Composite} from "./matter.min.js";
// or
import Matter from "./matter.min.js";

Is this already possible? If not, is it something worth adding?

Rosuav avatar Apr 21 '25 01:04 Rosuav

It's all in CJS so there is no way to do ESM import without an extra build / bundle step. Which is a bummer because it means this package doesn't actually run in a browser.

electrovir avatar May 07 '25 18:05 electrovir

If you pull in the library via a package manager then there's arguably no good reason these days not to include a bundling step. Modern bundlers will be able to automatically handle CJS imports in ES modules.

In other instances you can import it through bundling services like JSDelivr's esm.run or esm.sh, e.g.

import Matter from "https://esm.run/matter-js";

mootari avatar Jun 21 '25 15:06 mootari

If you pull in the library via a package manager then there's arguably no good reason these days not to include a bundling step.

I don't. Package managers are overkill; the browser already has module support, so why duplicate that?

In other instances you can import it through bundling services like JSDelivr's esm.run or esm.sh, e.g.

import Matter from "https://esm.run/matter-js";

Ah, now THAT looks more promising! Thank you. I just switched over to using this form. For anyone else following this thread, you can also do this:

import "https://esm.run/poly-decomp";

if you need concave object decomposition.

I'd definitely prefer to be able to (a) fetch directly from GitHub rather than adding another service dependency, or (b) use module imports with a vendored copy, but this is better than what I was doing previously with a script tag. Thanks.

Can this be added to the docs somewhere?

Rosuav avatar Jun 21 '25 21:06 Rosuav

Related Stack Overflow thread showing the jsdelivr alternative to esm.run:

import Matter from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'

ggorlen avatar Aug 12 '25 00:08 ggorlen