Skip to content

🔧 update: make sqlite runtime loading safe for node-targeted bundles - #2

Merged
warengonzaga merged 4 commits into
devfrom
copilot/fix-node-targeted-npm-clis
Aug 27, 2026
Merged

🔧 update: make sqlite runtime loading safe for node-targeted bundles#2
warengonzaga merged 4 commits into
devfrom
copilot/fix-node-targeted-npm-clis

Conversation

Copilot AI commented Mar 17, 2026

Copy link
Copy Markdown

Node-targeted CLIs consuming @wgtechlabs/config-engine could still fail at runtime because bundled output retained a bun:sqlite import. This change keeps Bun as the primary runtime while making the package safe to load from Node-targeted artifacts.

  • Runtime adapter loading

    • Replace the static Bun SQLite import path with runtime-only resolution.
    • Keep Node on better-sqlite3 via the existing adapter path.
    • Preserve the Bun-first behavior without leaking bun:* imports into Node bundles.
  • Bundling regression coverage

    • Add a focused runtime test that bundles a small entrypoint for target: "node".
    • Assert the emitted bundle does not contain a static bun:sqlite import and can be loaded by Node.
  • Documentation

    • Clarify that the Bun SQLite backend is loaded only at runtime on Bun, so Node-targeted bundles can consume the package safely.
import { createRequire } from "node:module";

const runtimeRequire = createRequire(import.meta.url);

function openBunDatabase(filepath: string) {
	const { Database } = runtimeRequire(getBunSqliteSpecifier());
	return new Database(filepath);
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Node-targeted npm CLIs fail because the package imports bun:sqlite</issue_title>
<issue_description>## Summary
Using @wgtechlabs/config-engine in a package that is built for Node and published to npm can fail at runtime because the package imports bun:sqlite.

In practice this makes the package Bun-only for bundled Node CLI releases, even when the consumer application itself targets Node.

Repro

Consumer build:

bun build src/index.ts --outfile dist/index.js --target node
node dist/index.js --version

Actual result

Node fails during module loading with:

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. Received protocol 'bun:'

Why this matters

I hit this in a released npm CLI because Bun bundled the dependency graph into a Node-targeted artifact, and the resulting build still contained bun:sqlite imports.

That means the package is not currently safe to consume from a published Node CLI unless the consumer is also effectively Bun-only.

Expected result

One of these should happen:

  1. The package provides a Node-safe backend or conditional runtime adapter.
  2. The package clearly documents that it is Bun-only.
  3. The package exposes a storage abstraction that allows Node consumers to avoid bundling bun:* imports.

Notes

I am happy to help test a fix from the consumer side.
</issue_description>

<agent_instructions>Let's add support to both node and bun, but primarily we support bun.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI and others added 2 commits March 17, 2026 14:18
Co-authored-by: warengonzaga <15052701+warengonzaga@users.noreply.github.com>
Co-authored-by: warengonzaga <15052701+warengonzaga@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Node-targeted npm CLIs failing due to bun:sqlite import Make SQLite runtime loading safe for Node-targeted bundles Mar 17, 2026
Copilot AI requested a review from warengonzaga March 17, 2026 14:21
@warengonzaga
warengonzaga changed the base branch from main to dev March 17, 2026 23:54
@warengonzaga
warengonzaga marked this pull request as ready for review July 4, 2026 12:01
Copilot AI review requested due to automatic review settings July 4, 2026 12:01
@warengonzaga warengonzaga changed the title Make SQLite runtime loading safe for Node-targeted bundles 🔧 update: make sqlite runtime loading safe for node-targeted bundles Jul 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR makes the SQLite backend selection safe for Node-targeted bundled artifacts by ensuring the Bun SQLite driver is only resolved at runtime, preventing bun:* imports from leaking into Node bundles.

Changes:

  • Switch Bun SQLite loading to runtime-only resolution (avoid static bun:sqlite imports).
  • Add a bundling-focused runtime test to validate Node-targeted output can be loaded and doesn’t include a static bun:sqlite import.
  • Document that the Bun SQLite backend is loaded only at runtime, improving guidance for Node consumers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/runtime.ts Uses runtime resolution to avoid static bun:sqlite imports in Node-targeted bundles while preserving Bun-first behavior.
tests/runtime.test.ts Adds a bundling regression test for Node-targeted output safety.
README.md Documents runtime-only Bun SQLite loading for Node-targeted bundle compatibility.

Comment thread tests/runtime.test.ts
… test

- Import openDatabase alongside isBun so the bundler cannot eliminate
  the SQLite adapter code, ensuring bun:sqlite guard is actually tested
- Switch to file:// URL for runtime path for cross-platform portability

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-project-automation github-project-automation Bot moved this to Backlog in Workbench Aug 27, 2026
@warengonzaga warengonzaga moved this from Backlog to In Review in Workbench Aug 27, 2026
@warengonzaga
warengonzaga merged commit f363962 into dev Aug 27, 2026
2 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in Workbench Aug 27, 2026
@warengonzaga
warengonzaga deleted the copilot/fix-node-targeted-npm-clis branch August 27, 2026 11:38
@warengonzaga warengonzaga added the data [Area] Database, storage, caching, or data models [issues, PRs] label Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data [Area] Database, storage, caching, or data models [issues, PRs]

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Node-targeted npm CLIs fail because the package imports bun:sqlite

3 participants