Skip to content

fix(core): actionable error for stale better-sqlite3 native binary - #582

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
all3f0r1:fix/better-sqlite3-node-version-error-562
Apr 15, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
all3f0r1:fix/better-sqlite3-node-version-error-562

Conversation

@all3f0r1

Copy link
Copy Markdown
Contributor

What does this PR do?

When Node.js is upgraded (or node_modules was built with a different Node version), better-sqlite3 fails to load with a NODE_MODULE_VERSION mismatch. Today, createDatabase catches this and surfaces a generic Failed to create database — the real cause is only visible if the user digs into the cause.

This PR detects the NODE_MODULE_VERSION string in the underlying error and throws an EmDashDatabaseError whose message tells the user exactly what to do: rebuild the native module with pnpm rebuild better-sqlite3 (or reinstall deps).

The helper is exported and covered by unit tests.

Closes #562

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code

Screenshots / test output

 ✓ tests/database/connection.test.ts (13 tests) 84ms
 Test Files  1 passed (1)
      Tests  13 passed (13)

… stale

When Node.js is upgraded without rebuilding native deps, better-sqlite3 throws
a NODE_MODULE_VERSION mismatch that createDatabase surfaces only as a generic
"Failed to create database". Detect this specific case and tell the user to
rebuild or reinstall the native module.

Closes #562

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@changeset-bot

changeset-bot Bot commented Apr 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 23d54cc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Apr 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@582

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@582

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@582

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@582

emdash

npm i https://pkg.pr.new/emdash@582

create-emdash

npm i https://pkg.pr.new/create-emdash@582

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@582

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@582

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@582

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@582

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@582

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@582

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@582

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@582

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@582

commit: 23d54cc

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice. Good catch

@ascorbic
ascorbic enabled auto-merge (squash) April 15, 2026 20:41
@ascorbic
ascorbic merged commit 04e6cca into emdash-cms:main Apr 15, 2026
26 checks passed
@emdashbot emdashbot Bot mentioned this pull request Apr 15, 2026
@all3f0r1
all3f0r1 deleted the fix/better-sqlite3-node-version-error-562 branch April 15, 2026 22:56
0aveRyan pushed a commit to 0aveRyan/emdash that referenced this pull request Apr 27, 2026
… stale (emdash-cms#582)

When Node.js is upgraded without rebuilding native deps, better-sqlite3 throws
a NODE_MODULE_VERSION mismatch that createDatabase surfaces only as a generic
"Failed to create database". Detect this specific case and tell the user to
rebuild or reinstall the native module.

Closes emdash-cms#562

Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
ascorbic added a commit that referenced this pull request Aug 27, 2026
…ter (#2106)

* fix(core): replace better-sqlite3 with node:sqlite in the sqlite adapter

better-sqlite3's natively compiled binary breaks deployments the moment
the environment and the binary disagree: NODE_MODULE_VERSION mismatches
after Node upgrades (#562, #582) and glibc version errors on shared
hosting where the OS is older than the build machine (#1833). Using the
Node.js built-in driver removes the native dependency entirely.

The sqlite() adapter and the CLI's createDatabase() now open databases
through node:sqlite's DatabaseSync, wrapped in a small better-sqlite3-
compatible shim (node-sqlite-compat.ts) so Kysely's built-in
SqliteDialect is unchanged. Statement .reader is derived from
StatementSync.columns(), preserving INSERT ... RETURNING semantics.

- better-sqlite3 moves to a devDependency (tests still use it directly)
- drops the NODE_MODULE_VERSION rebuild hint (no native binary anymore)
- removes better-sqlite3/bindings/file-uri-to-path from bundler externals
- emdash package now declares engines node >=22.15 (unflagged node:sqlite
  + StatementSync.columns())

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QSivQavm4D4nK86DgJBJCX

* Address review feedback on the node:sqlite adapter

Test harness: packages/core/tests/utils/test-db.ts created its SQLite
databases with better-sqlite3, so the suite never exercised the driver
the package actually ships. It now goes through openNodeSqliteDatabase.
The three tests that use better-sqlite3 directly keep it as a devDep.

Parameter binding: node:sqlite binds a narrower set of JS types than
better-sqlite3, and differs in both directions, so toBindings now maps
instead of casting.
  - undefined -> NULL. better-sqlite3 accepted it, node:sqlite throws.
    Reachable from ordinary Kysely usage (`.where(col, "=", undefined)`
    from an unset optional filter), so this was a real regression.
  - Date -> actionable TypeError. better-sqlite3 threw; node:sqlite
    silently binds NULL, turning a loud programming error into silent
    data loss.
  - boolean -> 0/1. Note the review's premise was inverted:
    better-sqlite3 12.8.0 also rejects booleans ("SQLite3 can only bind
    numbers, strings, bigints, buffers, and null"), so this is not a
    regression from this PR. It is a pre-existing bug — the plugin
    storage query API advertises `boolean` in WhereValue
    (plugins/types.ts) but threw at bind time on either driver — and
    mapping here fixes it.

Pragmas: journal_mode=WAL, busy_timeout=5000 and foreign_keys=ON move
into openNodeSqliteDatabase, the single place the package opens SQLite.
Previously only the CLI path set them, so sites on the runtime sqlite()
adapter silently got none of them.

Lockfile: regenerated with the repo's pinned pnpm (11.9.0 via corepack)
rather than the pnpm 9 that produced the original diff. pnpm 10+ reads
overrides/patchedDependencies from pnpm-workspace.yaml, which pnpm 9
ignores, which is why they were dropped from the lockfile — taking the
"@ungap/structured-clone" CWE-502 security pin and the @sigstore/core
patch with them. The lockfile diff against main is now only the
better-sqlite3 importer moves.

Also drops better-sqlite3 from the 9 templates/demos/fixtures that
listed it as a runtime dependency but never imported it, so new sites
no longer pull the native binary.

Verified: 4760 tests pass (the one failure, virtual-modules.test.ts,
fails identically on the unmodified PR tip — a macOS /var symlink
issue). Query-count harness matches its snapshots exactly, and WAL is
confirmed applied on the built fixture's data.db.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JSxrM3SUUGvrr9dzTQrbmF

* fix(core): replace better-sqlite3 with node:sqlite

Rebuilds and completes the approach proposed in #2106 against current main, with strict binding compatibility, preserved SQLite connection settings, static-prerender coverage, and updated packaging and documentation.\n\nCo-authored-by: gruntlord5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed to create database when running pnpm run bootstrap/seed due to NODE_MODULE_VERSION mismatch (better-sqlite3)

2 participants