Skip to content

Fix: externalize database drivers to prevent dynamic require error#291

Merged
tianzhou merged 1 commit intomainfrom
fix/issue-288-dynamic-require
Mar 28, 2026
Merged

Fix: externalize database drivers to prevent dynamic require error#291
tianzhou merged 1 commit intomainfrom
fix/issue-288-dynamic-require

Conversation

@tianzhou
Copy link
Copy Markdown
Member

Summary

  • Commit 1eefec4 moved database drivers to optionalDependencies with dynamic import(), but tsup bundled their CJS code into ESM chunks
  • At runtime, the bundled CJS require("events") calls fail with "Dynamic require of 'events' is not supported"
  • Adding the driver packages to tsup's external list ensures they're loaded from node_modules at runtime via standard ES imports

Changes

  • Add external: ['pg', 'mysql2', 'mariadb', 'mssql', 'better-sqlite3'] to tsup.config.ts

Closes #288

Test plan

  • pnpm run build:backend succeeds
  • Built output uses import pg from "pg" (external) instead of inlining pg internals
  • No Dynamic require / __require / createRequire shims in output
  • Runtime smoke test: no "Dynamic require" error loading dist/index.js
  • load-connectors.test.ts passes (10/10)

🤖 Generated with Claude Code

…o ESM

The optional database driver packages (pg, mysql2, etc.) were being bundled
by tsup into ESM chunks. When their internal CJS require() calls for Node.js
built-ins like "events" executed in the ESM context, it caused
"Dynamic require of 'events' is not supported" at runtime.

Marking them as external ensures they are loaded from node_modules at runtime
via standard import statements instead of being inlined.

Closes #288

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 28, 2026 13:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the backend build configuration to ensure DB driver packages remain unbundled so DBHub can dynamically load optional connector drivers at runtime without triggering Node ESM “Dynamic require” failures.

Changes:

  • Mark common database driver packages as external in tsup.config.ts to prevent bundling their CJS internals into ESM chunks.

// Database drivers are optionalDependencies loaded at runtime via dynamic
// import(). They must be external so tsup does not bundle their CJS code
// into ESM chunks (which causes "Dynamic require of X is not supported").
external: ['pg', 'mysql2', 'mariadb', 'mssql', 'better-sqlite3'],
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

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

external: ['mysql2', ...] does not externalize the subpath import used by the MySQL connector (import mysql from "mysql2/promise"). With esbuild/tsup, externals are exact-match unless you add patterns, so mysql2/promise may still get bundled and reintroduce the CJS-in-ESM require() runtime failure for the MySQL connector. Add mysql2/promise (or use a wildcard like mysql2/*) to the external list.

Suggested change
external: ['pg', 'mysql2', 'mariadb', 'mssql', 'better-sqlite3'],
external: ['pg', 'mysql2', 'mysql2/promise', 'mariadb', 'mssql', 'better-sqlite3'],

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Verified — esbuild (which tsup uses) treats external entries as prefix matches, not exact matches. external: ['mysql2'] already externalizes mysql2/promise.

Evidence from the current build output:

# dist/mysql-I35IQ2GH.js line 16:
import mysql from "mysql2/promise";

The mysql chunk is 469 lines (just connector code), not the thousands of lines mysql2 would add if bundled. No change needed here.

@tianzhou tianzhou merged commit b8d5e52 into main Mar 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[0.20.0] Fatal error: Error: Dynamic require of "events" is not supported

2 participants