Skip to content

Conversation

dynst
Copy link
Collaborator

@dynst dynst commented Jun 2, 2025

https://nodejs.org/api/packages.html#exports

Without this field, Node.js import logic believes it's just a CJS package - node doesn't recognize the old non-standard "module" field (while transpilers like webpack do).

Thorian1te
Thorian1te previously approved these changes Jun 3, 2025
Copy link
Collaborator

@Thorian1te Thorian1te left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fantastic

@dynst dynst marked this pull request as draft June 3, 2025 01:15
@dynst
Copy link
Collaborator Author

dynst commented Jun 3, 2025

Jest is tricky to get working with ESM code. jestjs/jest#13739

Specifically when tests are written in TypeScript. https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/

...and their recommended configuration requires a newer version of ts-jest than 29.1.0, which is from 2023. 29.2.0 added a bunch of ESM config helpers.

...and because Jest writes its own require() logic as part of its mocking feature, it's more restrictive than Node.js v20.19 which allows CJS code (the Jest test) to require() ESM code (the xchainjs module). Jest won't allow that, so the test has to be ESM too.

...except the tests aren't ESM. There are a whole ton of require() calls that are used to read and parse a nearby JSON file. They can't work in ESM:

    ReferenceError: require is not defined

@dynst dynst force-pushed the exports branch 3 times, most recently from ee58bf7 to 8bbb66c Compare June 3, 2025 11:22
@dynst dynst marked this pull request as ready for review June 3, 2025 13:36
@dynst dynst force-pushed the exports branch 6 times, most recently from f8edd47 to f97511f Compare June 3, 2025 17:50
@dynst
Copy link
Collaborator Author

dynst commented Jun 3, 2025

In order to make Jest work, since currently the tests only work when transpiled to CJS, and Jest refuses to let CJS code require ESM code, had to resort to conditional exports:

  "exports": {
    "require": "./lib/index.cjs",
    "import": "./lib/index.esm.js"
  },

@dynst dynst marked this pull request as draft June 4, 2025 18:20
@dynst
Copy link
Collaborator Author

dynst commented Jun 4, 2025

Never mind. The built ESM code in xchain-cosmos/kujira/thorchain/mayachain (and probably anything that depends on them) just doesn't work.

It only maybe works if a transpiler messes with it. Raw, it fails trying to import from cosmos/cosmjs-types#93

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'xchainjs-lib/node_modules/cosmjs-types/cosmos/tx/v1beta1/tx' imported from xchainjs-lib/packages/xchain-cosmos/lib/index.esm.js
Did you mean to import cosmjs-types/cosmos/tx/v1beta1/tx.js?

import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'

CosmJs packages are only very lightly maintained by Confio these days, and they're all effectively CommonJs-only right now. cosmos/cosmjs#1649 (comment)

Since it's not exported from the top level index.ts at all, the only ESM-compatible way to access that type is importing as .../tx.js.

@dynst dynst force-pushed the exports branch 11 times, most recently from 253594c to 7ecd27f Compare June 9, 2025 18:13
@dynst dynst force-pushed the exports branch 2 times, most recently from cc3cf32 to a1ebd02 Compare July 7, 2025 01:57
dynst added 3 commits July 10, 2025 16:40
9.1.0 is the version that added ESM support.
can't omit the file extension in ESM imports.
can't omit the file extension in ESM imports.
@dynst dynst force-pushed the exports branch 2 times, most recently from 69ea0a6 to 13f93b4 Compare July 10, 2025 18:01
@dynst dynst marked this pull request as ready for review July 10, 2025 18:07
@dynst dynst mentioned this pull request Jul 17, 2025
@dynst dynst marked this pull request as draft July 18, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants