matrix-org / matrix-js-sdk

Matrix Client-Server SDK for JavaScript
Apache License 2.0
1.55k stars 583 forks source link

matrix-js-sdk no longer works in CommonJS environment #4284

Closed richvdh closed 3 months ago

richvdh commented 3 months ago

As of https://github.com/matrix-org/matrix-js-sdk/pull/4187, which landed in v33.0.0, matrix-js-sdk is built into an ECMAScript module (ie, one which uses import {foo} from "bar" rather one that uses const {foo} = require("bar")).

This has broken things for people that attempt to import the SDK in a CommonJS environment, including one of our own examples.

Specifically, it results in this error:

(node:55093) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/rav/work/matrix-js-sdk/examples/node/node_modules/matrix-js-sdk/lib/index.js:17
import * as matrixcs from "./matrix";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1350:18)
    at Module._compile (node:internal/modules/cjs/loader:1379:20)
    at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
    at Module.load (node:internal/modules/cjs/loader:1249:32)
    at Module._load (node:internal/modules/cjs/loader:1065:12)
    at Module.require (node:internal/modules/cjs/loader:1271:19)
    at require (node:internal/modules/helpers:123:16)
    at Object.<anonymous> (/home/rav/work/matrix-js-sdk/examples/node/app.js:3:11)
    at Module._compile (node:internal/modules/cjs/loader:1434:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)

Node.js v22.2.0