Repository navigation
fix(node): load the platform package from the ESM build - #21
Merged
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
tryLoadPlatformPackage()used a barerequire(packageName). In the ESM bundle (dist/index.mjs) tsup replaces it with a__requireshim that throwsDynamic require of "..." is not supportedwhen no realrequireexists, and the emptycatchswallowed 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:
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;createRequire(import.meta.url): native ES modules, and webpack bundles that cannot follow a dynamicrequire(name). It is skipped when a bundler leftimport.meta.urlempty, andcreateRequireis reached through a namespace import so webpack does not rewrite the call statically.It moves to the next step only when
requireis unavailable or the package was not found. UsingcreateRequire(import.meta.url)alone would have brokenimportconsumers bundled to CommonJS (esbuild leavesimport.metaempty; webpack rewrites the call and then reports "not installed").No more swallowed errors: only a
MODULE_NOT_FOUNDfor the platform package itself is reported as "not installed". Any other load error is included in the message and attached asExtensionNotFoundError#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) installsdist/and a stub platform package into a temp project and loads it viaimportandrequire, 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 afternpm test, before publishing.esbuildis now an explicit devDependency (it was already present through tsup).Release
Bumps
SQLITE_JS_VERSIONinsrc/sqlitejs.hto 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.jsonis versioned by the release job, andPackage.swiftis updated by it after the assets are published.Verification
npm run build,npm run typecheckandnpx vitest run(7/7) pass.npm run test:dist: 20/20 pass. Againstmain'ssrc/index.tsit fails 12, including the reported ESM "is not installed" case. Against acreateRequire-only fix it fails the 4 "import, bundled to CJS" cases.importrequireimportimportrequireimportrequireimportrequire