fix: add shell wrapper to resolve bun/node ABI mismatch#320
Open
fix: add shell wrapper to resolve bun/node ABI mismatch#320
Conversation
When installed via bun, better-sqlite3 is compiled against bun's ABI (NODE_MODULE_VERSION 141). The previous hardcoded #!/usr/bin/env node shebang caused ERR_DLOPEN_FAILED on systems where node resolves to a standard Node.js binary (e.g. nvm-managed v22.x, module version 127). Add a bin/qmd shell wrapper that prefers bun when available, falling back to node. This ensures the runtime matches the ABI used during native module compilation. Fixes tobi#319
Using 'command -v bun' would prefer bun even for npm installs, potentially causing the same ABI mismatch in reverse. Instead, check if the package directory is under .bun/ to detect how it was installed and use the matching runtime.
38eb8f1 to
f8cdfdb
Compare
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
When installed via `bun install -g`, `better-sqlite3` is compiled against bun's internal ABI (`NODE_MODULE_VERSION 141`). The hardcoded `#!/usr/bin/env node` shebang in `dist/qmd.js` then causes every command to fail on systems where `node` resolves to a standard Node.js binary (e.g. nvm-managed v22.x, module version 127):
```
Error: The module '.../better_sqlite3.node'
was compiled against a different Node.js version using NODE_MODULE_VERSION 141.
This version of Node.js requires NODE_MODULE_VERSION 127.
```
The README presents bun and npm as equivalent install options, so this is easy to hit. Fixes #319.
Fix
Replaces the hardcoded shebang with a `bin/qmd` shell wrapper that detects the install method via path and picks the matching runtime:
```sh
#!/bin/sh
DIR="$(cd "$(dirname "$0")/.." && pwd)"
case "$DIR" in
/.bun/) exec bun "$DIR/dist/qmd.js" "$@" ;;
*) exec node "$DIR/dist/qmd.js" "$@" ;;
esac
```
If the package is installed under `.bun/` (i.e. via `bun install -g`), it runs under bun — matching the ABI used to compile the native addon. Otherwise it falls back to node.
Also updates `package.json` to point `bin.qmd` at `bin/qmd` and adds `bin/` to the `files` array.
Note: this aligns with the direction already described in `CLAUDE.md` ("The `qmd` file is a shell script that runs compiled JS from `dist/`").