Skip to content

Plugin fails to load on v2.14.3: require("bun:sqlite") in ESM scope in sqlite-bootstrap.js #113

@leiverkus

Description

@leiverkus

Summary

Sister bug to #112 (closed in 2.14.3). The JSON-import-attribute issue is fixed, but a second strict-ESM violation in dist/services/sqlite/sqlite-bootstrap.js keeps the plugin from loading on opencode ≥1.15 (Bun-based runtime).

Error

opencode log:

ERROR service=plugin path=opencode-mem
  target=file:///…/opencode-mem@latest/node_modules/opencode-mem/dist/plugin.js
  error=require is not defined in ES module scope, you can use import instead
  in dist/services/sqlite/sqlite-bootstrap.js:4
    const bunSqlite = require("bun:sqlite");
failed to load plugin

Root Cause

src/services/sqlite/sqlite-bootstrap.ts mixes a CommonJS require() call into an ESM module (parent package.json has "type": "module"):

let Database: typeof import("bun:sqlite").Database;

export function getDatabase(): typeof import("bun:sqlite").Database {
  if (!Database) {
    const bunSqlite = require("bun:sqlite") as typeof import("bun:sqlite");
    Database = bunSqlite.Database;
  }
  return Database;
}

Under Bun's strict-ESM mode (used by opencode's embedded Bun runtime in 1.15.x), require is not available in ESM scope. The compiled dist/services/sqlite/sqlite-bootstrap.js carries the same require() call verbatim.

Fix

One-line change — replace require() with a top-level static import (which works fine because bun:sqlite is a Bun built-in):

import { Database as BunDatabase } from "bun:sqlite";

let Database: typeof BunDatabase;
export function getDatabase(): typeof BunDatabase {
  if (!Database) Database = BunDatabase;
  return Database;
}

I verified the workaround locally by overwriting the cached dist/services/sqlite/sqlite-bootstrap.js with the equivalent ESM-only version — plugin now loads cleanly on opencode 1.15.10 / Bun-v1.3.14 / macOS arm64.

Environment

  • macOS 26.3 (Apple Silicon, arm64)
  • opencode 1.15.10 (Homebrew anomalyco/opencode, embeds bun-v1.3.14)
  • opencode-mem 2.14.3 (latest), installed via opencode's plugin cache at ~/.cache/opencode/packages/opencode-mem@latest/

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions