nodejs-sqlite

Updated

Using SQLite with Node.js has become more accessible with several mature options depending on your project's needs.

1. Built-in node:sqlite (Node.js 22+)

Starting with Node.js v22.5.0, a native, experimental SQLite module is included in the core runtime.

  • Best for: Projects where you want to minimize external dependencies.
  • Key Feature: Uses the DatabaseSync class for synchronous database operations.
  • Usage:
    import { DatabaseSync } from 'node:sqlite';
    const db = new DatabaseSync(':memory:');
    db.exec(`CREATE TABLE data (key INTEGER PRIMARY KEY, value TEXT)`);

2. better-sqlite3 (Recommended Third-Party)

For most production applications, better-sqlite3 is widely considered the "gold standard" for SQLite in Node.js.

  • Best for: High performance and simplicity.
  • Key Advantage: It uses a synchronous API, which is faster for SQLite’s local, in-process nature than the asynchronous wrappers found in other libraries. It avoids the event-loop overhead that can occur with node-sqlite3.
  • Installation: npm install better-sqlite3

3. sqlite3

This is the "original" library for Node.js.

  • Best for: Projects that strictly require an asynchronous, callback-based API.
  • Key Characteristic: It is stable and battle-tested but generally slower than better-sqlite3 due to its asynchronous design.
  • Installation: npm install sqlite3

Summary Comparison

Library API Style Performance Status
node:sqlite Synchronous High Experimental (Built-in)
better-sqlite3 Synchronous Very High Industry Favorite
sqlite3 Asynchronous Moderate Classic / Stable

Recommendation: If you are using Node.js 22+, node:sqlite is a great zero-dependency choice for simple tasks. If you need a mature, high-performance solution for a complex application, better-sqlite3 remains the most recommended library.