#649

CJS bundle: sqlite3 fails with "worker failed to load" when loaded with require()

OpenSebastianBodzaopened 11d ago

just-bash version

3.6.0 (also reproduced on 2.14.5)

Reproduction

// repro.cjs
const { Bash } = require("just-bash");

(async () => {
  const bash = new Bash();
  console.log(JSON.stringify(await bash.exec('sqlite3 :memory: "SELECT 1;"')));
  console.log(JSON.stringify(await bash.exec("sqlite3 -version")));
})();

The same script loaded through ESM (import { Bash } from "just-bash", resolving to dist/bundle/index.js) works. Only the require export (dist/bundle/index.cjs) is affected.

How you run it

node repro.cjs (plain Node, no bundler)

Expected behavior

Same result as the ESM entry:

{"stdout":"1\n","stderr":"","exitCode":0}
{"stdout":"3.54.0\n","stderr":"","exitCode":0}

Actual behavior

{"stdout":"","stderr":"sqlite3: worker error: sqlite3 worker failed to load: The \"path\" argument must be of type string or an instance of URL. Received undefined\n","exitCode":1}
{"stdout":"3.54.0\n","stderr":"","exitCode":0}

sqlite3 -version works because it does not spawn the worker; every query fails.

Cause

findWorkerPath in packages/just-bash/src/commands/sqlite3/sqlite3.ts defaults to dirname(fileURLToPath(import.meta.url)). In the esbuild --format=cjs bundle import.meta becomes {}, so import.meta.url is undefined and fileURLToPath throws ERR_INVALID_ARG_TYPE.

Using __dirname alone would not be enough: in the CJS bundle the worker lives at dist/bundle/chunks/sqlite3-worker.js, and none of the current candidates (sqlite3-worker.js, ../../commands/sqlite3/worker.js, ../../../dist/commands/sqlite3/worker.js) resolve to it from dist/bundle.

Suggested fix

Same approach as #437 does for python3: resolve the module directory via __dirname when running as CJS (falling back to import.meta.url otherwise) and add a chunks/sqlite3-worker.js candidate. A shared worker-path helper for js-exec / python3 / sqlite3 would avoid the same bug in each command, and the CJS smoke test from #437 (src/bundle-cjs.test.ts) could cover a sqlite3 query as well.

Related: #385 (js-exec, same root cause), #437 (python3 fix).

Workaround: load just-bash via ESM (import) instead of require().

Environment

- Runtime: Node.js, CommonJS (`require("just-bash")`)
- Runtime version: 24.13.0 (also seen on Node 22 in an Azure Functions app compiled to CommonJS)
- Operating system: Windows
- Bundler or framework: none for the repro; originally TypeScript `module: commonjs` output

Activity

0

No activity yet.