ScriptsApr 28, 2026·3 min read

sql.js — Run SQLite in the Browser with WebAssembly

A JavaScript library that compiles SQLite to WebAssembly, letting you run a full SQL database entirely in the browser or Node.js.

Agent ready

Safe staging for this asset

This asset is staged first. The copied prompt tells the agent to inspect the staged files and ask before activating scripts, MCP config, or global config.

Stage only · 17/100Policy: stage
Agent surface
Any MCP/CLI agent
Kind
Script
Install
Stage only
Trust
Trust: Established
Entrypoint
sql.js
Safe staging command
npx -y tokrepo@latest install 403326a8-429a-11f1-9bc6-00163e2b0d79 --target codex

Stages files first; activation requires review of the staged README and plan.

Introduction

sql.js is SQLite compiled to WebAssembly via Emscripten. It gives you a complete, standards-compliant SQL database that runs entirely in the browser or in Node.js without any native dependencies. This is useful for client-side data processing, offline apps, and interactive SQL playgrounds.

What sql.js Does

  • Provides a full SQLite engine compiled to WebAssembly
  • Executes standard SQL queries including joins, indexes, and triggers
  • Opens and exports .sqlite / .db files as binary arrays
  • Runs in browsers, Node.js, Deno, and web workers
  • Requires no server, native bindings, or build toolchain

Architecture Overview

sql.js uses Emscripten to compile the official SQLite C source into a WASM binary. The JavaScript wrapper loads this binary and provides a synchronous API that mirrors the SQLite C interface. The database lives in a WASM linear memory buffer. You can import an existing database from a Uint8Array and export it back for persistence.

Self-Hosting & Configuration

  • Install via npm or load the WASM binary from a CDN
  • Initialize with initSqlJs() which fetches and compiles the WASM module
  • Pass locateFile to customize where the WASM binary is loaded from
  • For persistence, export the database to a Uint8Array and save it to IndexedDB, localStorage, or a file
  • Run heavy queries in a Web Worker to avoid blocking the main thread

Key Features

  • Full SQLite feature set including FTS5, JSON1, and R-Tree extensions
  • No native dependencies — works anywhere JavaScript runs
  • Import and export .sqlite files as binary arrays
  • Synchronous API makes sequential SQL operations straightforward
  • WASM binary is under 1 MB gzipped

Comparison with Similar Tools

  • Dexie.js — IndexedDB wrapper with a NoSQL-style API; sql.js provides full SQL
  • PGlite — embedded Postgres in WASM; sql.js uses SQLite which is lighter
  • better-sqlite3 — native Node.js binding; sql.js works in browsers without native code
  • absurd-sql — persistence layer on top of sql.js using IndexedDB as a VFS

FAQ

Q: Is data persisted across page reloads? A: Not automatically. The database lives in memory. Export it to IndexedDB or the Origin Private File System for persistence.

Q: How large of a database can sql.js handle? A: It depends on available WASM memory. Databases of tens of megabytes work well; for larger datasets, consider a server-side database.

Q: Can I use existing .sqlite files? A: Yes. Pass the file as a Uint8Array to the Database constructor to open it.

Q: Is sql.js slower than native SQLite? A: WASM adds some overhead, but for typical workloads the difference is small. CPU-intensive queries on large tables will be noticeably slower.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets