Registry / database / better-sqlite3

better-sqlite3

JSON →
library12.10.0jsnpmunverified

The fastest and simplest SQLite library for Node.js, with a synchronous API that outperforms alternatives like sqlite3 by 2-24x in common operations. Version 12.10.0 requires Node.js 20+ and includes prebuilt binaries for LTS. Key differentiators: full transaction support, user-defined functions/aggregates/virtual tables, 64-bit integer support, and worker thread compatibility. The synchronous API is designed to be safer and more efficient than async equivalents for SQLite's inherently single-threaded nature.

npm install better-sqlite3
INSTALL
IMPORT
SIG · BETTER-SQLITE3
B
better-sqlite3
databasejavascriptv12.10.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Database
import Database from 'better-sqlite3';
const { Database } = require('better-sqlite3');
Default export is a function/constructor. Do not destructure.
default import (CommonJS)
const Database = require('better-sqlite3');
const db = require('better-sqlite3')('path');
CommonJS require returns the constructor directly; you can call it immediately.
Database type (TypeScript)
import type Database from 'better-sqlite3';
import Database from 'better-sqlite3'; // if only type needed
Use import type for type-only imports to avoid runtime side effects.
Statement
import type { Statement } from 'better-sqlite3';
Statement type is exported for TypeScript; use with prepared statement type annotations.

Creates in-memory database, enables WAL, creates table, inserts rows in transaction, and queries a single row.

import Database from 'better-sqlite3'; const db = new Database(':memory:'); db.pragma('journal_mode = WAL'); db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)'); const insert = db.prepare('INSERT INTO users (name) VALUES (?)'); const insertMany = db.transaction((names) => { for (const name of names) insert.run(name); }); insertMany(['Alice', 'Bob', 'Charlie']); const row = db.prepare('SELECT * FROM users WHERE id = ?').get(1); console.log(row.name); db.close();
Debug
Known issues
gotchaRunning queries that take a long time blocks the event loop entirely - synchronous API can freeze Node.js for the duration.
fix
Use worker threads for long queries, or ensure queries are fast (indexed, small).
affects: >=0.0.0
gotchaOpening a database with WAL mode enabled is strongly recommended for performance, but not default.
fix
Always call db.pragma('journal_mode = WAL') after opening.
affects: >=0.0.0
gotchaDatabase handle must be closed manually with db.close() to avoid resource leaks; not garbage-collected automatically.
fix
Call db.close() when done, or use a wrapper with cleanup.
affects: >=0.0.0
breakingVersion 9.0.0 dropped support for Node.js < 12 and changed the default output of integer columns from Number to BigInt.
fix
Use db.defaultSafeIntegers(true) to restore old behavior, or handle BigInt values.
affects: >=9.0.0 <10.0.0
deprecatedThe `.pluck()` method is deprecated in favor of passing options to `.all()`, `.get()`, etc.
fix
Use db.prepare('sql').all({ pluck: true }) instead of .pluck().all().
affects: >=11.0.0
Errors
Common errors & fixes
Error: Cannot find module 'better-sqlite3'
Global install or missing local install
fix
Install locally: npm install better-sqlite3
Error: This version of better-sqlite3 requires Node.js 20.x or higher (current: 14.x)
Outdated Node.js version
fix
Upgrade Node.js to 20+ or use an older version of better-sqlite3 (but note that older versions are unsupported).
TypeError: db.prepare().get is not a function
Using .prepare() result incorrectly (e.g., not chaining .get())
fix
Use const stmt = db.prepare('...'); const row = stmt.get();
Upgrade
Version history
12.10.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
better-sqlite3 — npm install better-sqlite3 · libregistry