Skip to content

fix(node): load the platform package from the ESM build - #21

Merged
Gioee merged 3 commits into
mainfrom
fix/node-esm-platform-package
Nov 12, 2026
Merged

Gioee merged 3 commits into
mainfrom
fix/node-esm-platform-package

Conversation

@Gioee

@Gioee Gioee commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Problem

The README's Node.js sample, run as an ES module, cannot load the extension, and the error points at a package that is already installed:

ExtensionNotFoundError: SQLite JS extension not found for platform: linux-x86_64
The platform-specific package "@sqliteai/sqlite-js-linux-x86_64" is not installed.

tryLoadPlatformPackage() used a bare require(packageName). In the ESM bundle (dist/index.mjs) tsup replaces it with a __require shim that throws Dynamic require of "..." is not supported when no real require exists, and the empty catch swallowed that error, so the message blamed a missing package. The CommonJS entry point was unaffected.

Changes

  • Fix: the platform package is loaded by trying, in order:

    1. require: available in the CommonJS build and in any CommonJS bundle of the ESM build (esbuild, webpack), where it resolves relative to the running bundle;
    2. createRequire(import.meta.url): native ES modules, and webpack bundles that cannot follow a dynamic require(name). It is skipped when a bundler left import.meta.url empty, and createRequire is reached through a namespace import so webpack does not rewrite the call statically.

    It moves to the next step only when require is unavailable or the package was not found. Using createRequire(import.meta.url) alone would have broken import consumers bundled to CommonJS (esbuild leaves import.meta empty; webpack rewrites the call and then reports "not installed").

  • No more swallowed errors: only a MODULE_NOT_FOUND for the platform package itself is reported as "not installed". Any other load error is included in the message and attached as ExtensionNotFoundError#cause (non-enumerable, like the native one), and a package that does not export an existing binary path gets its own message.

  • Smoke test for the built output: npm run test:dist (scripts/smoke-test-dist.mjs) installs dist/ and a stub platform package into a temp project and loads it via import and require, both directly and after bundling the consumer with esbuild to CJS and ESM. It covers: installed, not installed, a package that throws on load, and a missing binary. The release job runs it after npm test, before publishing. esbuild is now an explicit devDependency (it was already present through tsup).

Release

Bumps SQLITE_JS_VERSION in src/sqlitejs.h to 1.4.1 and adds the changelog entry, so merging this PR triggers the automatic release. It is a patch release: no API changes. packages/node/package.json is versioned by the release job, and Package.swift is updated by it after the assets are published.

Verification

  • npm run build, npm run typecheck and npx vitest run (7/7) pass.
  • npm run test:dist: 20/20 pass. Against main's src/index.ts it fails 12, including the reported ESM "is not installed" case. Against a createRequire-only fix it fails the 4 "import, bundled to CJS" cases.
  • Manual bundler matrix with a real platform-package layout:
Consumer Output main this PR
plain Node import ❌ ✅
plain Node require ✅ ✅
esbuild import CJS ✅ ✅
esbuild import ESM ❌ ✅
esbuild require CJS ✅ ✅
webpack 5 import commonjs2 ✅ ✅
webpack 5 require commonjs2 ❌ ✅
webpack 5 import ESM ❌ ✅
webpack 5 require ESM ❌ ✅

Importing @sqliteai/sqlite-js as an ES module always threw
ExtensionNotFoundError claiming the platform package was not installed,
even when it was. The loader used a bare `require`, which the ESM bundle
replaces with a shim that throws "Dynamic require ... is not supported";
an empty catch then swallowed that error.

- Load the platform package through createRequire(import.meta.url),
  which works in both the ESM and the CommonJS builds.
- Only treat MODULE_NOT_FOUND for the platform package itself as "not
  installed". Any other load error is now reported in the message and
  attached as `cause`, and a package without a valid binary path gets
  its own message.
- Add a smoke test (npm run test:dist) that loads the built dist through
  both import and require against a stub platform package, and run it in
  the release job before publishing.
Resolving the platform package only through createRequire(import.meta.url)
broke consumers that `import` the package and bundle to CommonJS: esbuild
leaves import.meta empty, and webpack rewrites the call statically and
then reports the package as not installed.

Try `require` first, which exists at runtime in the CommonJS build and in
any CommonJS bundle, and fall back to createRequire(import.meta.url) when
`require` is unavailable (native ESM) or cannot find the package (webpack
bundles of the CommonJS build). createRequire is reached through a
namespace import so webpack leaves it alone, and it is skipped when a
bundler left import.meta.url empty.

The smoke test now also runs the consumers after bundling them with
esbuild to CommonJS and ESM, and imports dist through a file URL so it
works on Windows. esbuild is now an explicit devDependency, and
ExtensionNotFoundError#cause is non-enumerable like the native one.
@Gioee
Gioee marked this pull request as ready for review November 12, 2026 15:12
Bump SQLITE_JS_VERSION so merging releases the Node.js fix in this
branch, and record it in the changelog. It is a patch release: the
public API is unchanged, and the only difference users can observe
besides the fix is the more precise ExtensionNotFoundError message.

Package.swift is left alone, since the release job rewrites its URL and
checksum once the assets exist.
@Gioee
Gioee merged commit 126690b into main Nov 12, 2026
14 checks passed
@marcobambini
marcobambini deleted the fix/node-esm-platform-package branch November 16, 2026 09:04
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.

1 participant