Skip to content

Build the Node package for Node's own ESM loader - #475

Merged
ospfranco merged 1 commit into
OP-Engineering:mainfrom
arvidtorbjoernsen:fix/node-esm-extensions
Oct 28, 2026
Merged

ospfranco merged 1 commit into
OP-Engineering:mainfrom
arvidtorbjoernsen:fix/node-esm-extensions

Conversation

@arvidtorbjoernsen

Copy link
Copy Markdown
Contributor

Fixes #474.

node/package.json is "type": "module", so Node loads node/dist as ESM, and Node's ESM loader needs a full path in every relative import. The Node build used "moduleResolution": "bundler", which let ./database through without an extension, so import('@op-engineering/op-sqlite') in Node failed with ERR_MODULE_NOT_FOUND.

On the nodenext question in the issue: TypeScript deprecated the old node10/node resolution, not node16/nodenext. nodenext is TypeScript's documented setting for code that Node itself loads, which is what node/dist is. bundler stays right for the React Native package, which Metro bundles, and this PR leaves it alone: the root tsconfig.json excludes node/.

The change (node/ only):

  • node/tsconfig.json: "module" and "moduleResolution" are "NodeNext".
  • The relative imports in node/src name their .js files (./database.js, ./types.js, ./index.js). TypeScript now checks that each emitted import resolves under Node.
  • Jest needs no change: node/jest.config.js already maps .js imports back to the .ts sources.

Checked, building node/ with the repo's TypeScript (7.0.2) on Node 26:

  • Before: dist/index.js imports ./database, and loading it fails with ERR_MODULE_NOT_FOUND.
  • After: dist/index.js imports ./database.js, and Node loads it with all its exports (Database, open, openSync, openV2, …).
  • node/src/test.spec.ts, run from its compiled dist/test.spec.js with Jest: 18 pass, before and after. Jest's own resolver accepts extensionless imports, which is why the tests never showed this.
  • The rest of the repo, after yarn install from the lockfile, on main and on this branch:
    • yarn typecheck (CI's check) passes on both.
    • yarn prepare (bob build && yarn build:node) builds both packages.
    • npm pack ships the same node/dist files as before.
    • yarn lint reports the same two errors on both, in docs/tsconfig.json and test_transaction.js, which this PR doesn't touch.
  • Not run: CI's iOS and Android example-app jobs. They build the React Native package, which this PR doesn't change.

One thing I noticed but didn't touch: yarn test:node can't run on main as it is, because ts-jest needs TypeScript's JavaScript API, which TypeScript 7 doesn't ship ("does not expose the JavaScript compiler API required by ts-jest"). That happens with or without this PR.

🤖 Generated with Claude Code

node/package.json is "type": "module", so Node loads node/dist as ESM,
and its loader needs a full path in each relative import. The build used
"moduleResolution": "bundler", which let "./database" through without an
extension, so importing the package in Node failed with ERR_MODULE_NOT_FOUND.

The Node build now compiles with "module" and "moduleResolution"
"NodeNext", TypeScript's setting for code Node loads directly, and its
relative imports name their ".js" files. The React Native package keeps
"bundler" in the root tsconfig, which excludes node/.

Fixes OP-Engineering#474

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ospfranco
ospfranco merged commit 5b23a67 into OP-Engineering:main Oct 28, 2026
9 of 10 checks passed
@ospfranco

Copy link
Copy Markdown
Contributor

Thanks for the PR!

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.

[Bug]: the Node build's ESM output imports ./database without an extension, so Node can't load it

2 participants