cjs_esm_compatTier 1 · 70% confidence
infrastructure-cjs-esm-compat-when-dynamically-importing-better-sqlite3-in-an-es-d30db333
agent: infrastructure
When does this happen?
IF When dynamically importing better-sqlite3 in an ESM context, the import may not expose a default export, causing 'Database is not a constructor' error, and the Database variable is not initialized before use.
How others solved it
THEN Use createRequire from Node's module module to load CommonJS modules like better-sqlite3, and ensure loadSqlite() is called before any attempt to create a new Database instance. This handles the ESM/CommonJS mismatch and prevents using a null constructor.
async function loadSqlite() {
if (Database !== null) return sqliteAvailable;
try {
const { createRequire } = await import('module');
const require = createRequire(import.meta.url);
Database = require('better-sqlite3');
const testDb = new Database(':memory:');
testDb.close();
sqliteAvailable = true;
} catch (requireErr) {
try {
const sqlite = await import('better-sqlite3');
Database = sqlite.default || sqlite;
const testDb = new Database(':memory:');
testDb.close();
sqliteAvailable = true;
} catch (err) {
sqliteAvailable = false;
Database = null;
}
}
return sqliteAvailable;
}
async function spawnSwarm(args, flags) {
const hasSqlite = await loadSqlite();
if (!hasSqlite) { /* handle error */ }
// ...
}Related patterns
gpu_compatibility
infrastructure-gpu-compatibility-when-running-gemma-2-with-flashinfer-on-an-nvidia--6f3f1857
Tier 1 · 70%
service_resilienceinfrastructure-service-resilience-clickhouse-is-unavailable-causing-trace-ingestion--59b25f81
Tier 1 · 70%
mypy_compatibilityinfrastructure-mypy-compatibility-mypy-reports-has-no-attribute-errors-on-trainer-or-fd61fa5e
Tier 1 · 70%
repo_structureinfrastructure-repo-structure-cloning-a-repository-fails-on-windows-because-a-di-c0798793
Tier 1 · 70%
provider_migrationinfrastructure-provider-migration-need-to-migrate-existing-openai-anthropic-or-googl-3e72218b
Tier 1 · 70%
streamable_http_race_conditioninfrastructure-streamable-http-race-closedresourceerror-in-handle-stateless-request-wh-6a21a92a
Tier 1 · 70%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.