Skip to content

use "exports" field in package.json #1410

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

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft

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'

It looks like there's no actual ESM-compatible way to access that type, because it's not even exported from the top level index.ts.

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

@dynst dynst force-pushed the exports branch 9 times, most recently from 5d20c52 to 38fb86c Compare June 5, 2025 22:26
dynst added 3 commits June 6, 2025 22:19
Its CommonJS code is so convoluted that node can't statically
determine the named exports.
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