Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrix-js-sdk no longer works in node #4287

Closed
richvdh opened this issue Jun 26, 2024 · 10 comments · Fixed by #4377
Closed

matrix-js-sdk no longer works in node #4287

richvdh opened this issue Jun 26, 2024 · 10 comments · Fixed by #4377
Labels
A-Developer-Experience O-Occasional Affects or can be seen by some users regularly or most users rarely S-Major Severely degrades major functionality or product features, with no satisfactory workaround T-Defect

Comments

@richvdh
Copy link
Member

richvdh commented Jun 26, 2024

#4284 describes a problem wherein matrix-js-sdk no longer works in a CommonJS environment, but actually it's worse than that: it doesn't work in an ES modules environment either.

If we apply the fix from #4285, and run in an esm environment, I get:

node:internal/modules/esm/resolve:260
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/rav/work/matrix-js-sdk/examples/node/node_modules/matrix-js-sdk/lib/matrix' imported from /home/rav/work/matrix-js-sdk/examples/node/node_modules/matrix-js-sdk/lib/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:260:11)
    at moduleResolve (node:internal/modules/esm/resolve:920:10)
    at defaultResolve (node:internal/modules/esm/resolve:1119:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:511:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:241:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///home/rav/work/matrix-js-sdk/examples/node/node_modules/matrix-js-sdk/lib/matrix'
}

Node.js v22.2.0

The problem appears to be that index.js contains:

import * as matrixcs from "./matrix";

However, the file it actually wants is ./matrix.js, and the nodejs ESM resolver doesn't know it needs to add the extension. Likewise for every link in the project :/

@richvdh
Copy link
Member Author

richvdh commented Jun 26, 2024

Apparently, the typescript way to do this is actually to

import {foo} from "./matrix.js"`; // note js extension

The .js is then retained in the output, but typescript is intelligent enough to use the ts file for type definitions (cf https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution)

@dbkr dbkr added O-Occasional Affects or can be seen by some users regularly or most users rarely S-Major Severely degrades major functionality or product features, with no satisfactory workaround labels Jul 3, 2024
@0ctoPusK
Copy link

0ctoPusK commented Jul 4, 2024

I got the same error as you had

@richvdh
Copy link
Member Author

richvdh commented Aug 22, 2024

If anybody would like to go through all the import statements in the js-sdk source and add the .js suffix to all the filenames, I'd happily review a pull-request.

Johennes added a commit to Johennes/matrix-js-sdk that referenced this issue Aug 22, 2024
@Johennes
Copy link
Contributor

Bit of a wild idea but maybe we could just tweak the imports with babel? I haven't tested this a lot but #4373 at least lets me run the Node.js example locally.

Johennes added a commit to Johennes/matrix-js-sdk that referenced this issue Aug 23, 2024
@Johennes
Copy link
Contributor

Alternatively, #4375 script-replaces all imports. This also lets me run the Node.js example but appears to break most test.

@richvdh
Copy link
Member Author

richvdh commented Aug 23, 2024

SIGH. How can this possibly be so difficult? I can't believe we are the first library ever to use typescript and jest, and transpile to ES modules.

Maybe what we want is to have the source say import {foo} from "./matrix.ts" and enable https://babeljs.io/docs/babel-preset-typescript#rewriteimportextensions, except when process.env.NODE_ENV === "test" ?

Johennes added a commit to Johennes/matrix-js-sdk that referenced this issue Aug 23, 2024
@Johennes
Copy link
Contributor

Maybe what we want is to have the source say import {foo} from "./matrix.ts" and enable https://babeljs.io/docs/babel-preset-typescript#rewriteimportextensions, except when process.env.NODE_ENV === "test" ?

This looks promising: #4377

@ledlamp
Copy link

ledlamp commented Aug 27, 2024

omg. How do I use this library then???

@Johennes
Copy link
Contributor

You might be able to tweak imports by using a bundler in your project until #4377 lands and is released.

github-merge-queue bot pushed a commit that referenced this issue Aug 29, 2024
* Change imports for Node.js compatibility

Fixes: #4287
Signed-off-by: Johannes Marbach <[email protected]>

* Run prettier

* Run prettier (again)

* Add comment

* Update babel.config.cjs

---------

Signed-off-by: Johannes Marbach <[email protected]>
Co-authored-by: Richard van der Hoff <[email protected]>
@richvdh
Copy link
Member Author

richvdh commented Sep 5, 2024

This should be fixed in v34.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Developer-Experience O-Occasional Affects or can be seen by some users regularly or most users rarely S-Major Severely degrades major functionality or product features, with no satisfactory workaround T-Defect
Projects
None yet
5 participants