SQLite compiled to JavaScript via Emscripten, enabling full SQLite functionality in browsers and Node.js without native bindings. Current stable version is 1.14.1, released periodically. Key differentiator: runs entirely in memory using WebAssembly (or legacy JS), allows importing/exporting SQLite database files as Uint8Array, and works cross-platform without native dependencies. Includes contributed math/string extension functions. Note: unlike native SQLite bindings (e.g., sqlite3), sql.js requires loading the entire database into memory, which can cause out-of-memory issues for large databases. Pure JavaScript implementation with WebAssembly fallback.
npm install sql.jsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates creating an in-memory SQLite database, running statements, using prepared statements with parameter binding, and exporting the database as a Uint8Array.
Call db.export() to get a Uint8Array and persist it (e.g., write to file or localStorage). To import, pass the Uint8Array to new SQL.Database(data).
Use await initSqlJs() or .then() instead of passing a callback.
Set locateFile property in initSqlJs config to point to the correct URL or local path of the .wasm file.
Use the default WebAssembly build (sql-wasm.js) by importing 'sql.js' which now uses WASM. For legacy browsers, use the 'sql-asm.js' variant explicitly.
Always call stmt.free() after processing all rows, or use try/finally to ensure cleanup.
Explicitly set locateFile in initSqlJs options, or ensure the wasm file is in the expected path relative to the script.
const SQL = await initSqlJs({ locateFile: ... });Ensure the .wasm file matches the sql.js version. Use the same source for both (e.g., from npm).
Provide a valid locateFile function that returns the correct URL or local path to sql-wasm.wasm.
Use the legacy JavaScript build (sql-asm.js) instead of the WASM build, or ensure the environment supports SharedArrayBuffer (requires cross-origin isolation headers).