Registry / database / sqlite3-parser

sqlite3-parser

JSON →
library0.7.1jsnpmunverified

A fast, pure JavaScript LALR(1) parser for SQLite SQL syntax, generated from SQLite's parse.y grammar. Version 0.7.1 ships TypeScript types, runs in Node, Bun, and browsers, and is ~32 KB gzipped. It provides improved error messages with location, expected tokens, and common mistake hints. Compared to alternatives like sql.js or node-sqlite3, it is purely a parser (no database connection) and is 2x-200x faster than other JavaScript SQL parsers (e.g., @nene/query-lang, sql-parser-cst).

npm install sqlite3-parser
INSTALL
IMPORT
SIG · SQLITE3-PARSER
S
sqlite3-parser
databasejavascriptv0.7.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

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

parse
import { parse } from 'sqlite3-parser'
const parse = require('sqlite3-parser').parse
ESM-only since v0.7.0; CommonJS require is not supported. Use dynamic import or bundler with ESM interop.
parseStmt
import { parseStmt } from 'sqlite3-parser'
const { parseStmt } = require('sqlite3-parser')
Same ESM-only constraint. TypeScript users must ensure moduleResolution is 'node16' or 'bundler'.
parseOrThrow
import { parseOrThrow } from 'sqlite3-parser'
Convenience function that throws on parse error. Useful when you know the SQL is valid.
type ParseOk
import type { ParseOk } from 'sqlite3-parser'
import { ParseOk } from 'sqlite3-parser'
ParseOk is a type-only export. Use `import type` to avoid runtime errors in isolated modules.
type ParseDiagnostic
import type { ParseDiagnostic } from 'sqlite3-parser'
Error diagnostics are not subclasses of Error. Always check `result.status` before accessing error info.

Demonstrates parsing multiple statements, single statement, throwing on error, and incremental parsing with allowTrailing option.

import { parse, parseStmt, parseOrThrow } from 'sqlite3-parser'; // Parse multiple statements const multi = parse(`SELECT * FROM t1; INSERT INTO t2 VALUES (1)`); if (multi.status === 'ok') { console.log(multi.root.cmds.length); // 2 console.log(multi.root.cmds[0].type); // SelectStmt } // Parse single statement const single = parseStmt('SELECT id FROM users WHERE name = ?'); if (single.status === 'ok') { console.log(single.root.type); // SelectStmt } // Parse or throw const stmt = parseOrThrow('UPDATE t SET x = 1'); console.log(stmt.root.type); // CmdList (always contains one command) // Incremental parsing with allowTrailing const result = parseStmt('SELECT 1; SELECT 2', { allowTrailing: true }); if (result.status === 'ok') { console.log(result.tail); // 9 (index where next statement starts) }
Debug
Known issues
breakingESM-only since v0.7.0. Deep require('sqlite3-parser') will fail with ERR_REQUIRE_ESM.
fix
Use import syntax, or upgrade to Node >=14 and use dynamic import.
affects: >=0.7.0
gotchaParseDiagnostic is not a subclass of Error; catching 'instanceof Error' will not catch parse errors.
fix
Always check result.status === 'ok' before using result.root. Use parseOrThrow for exception-based flow.
affects: >=0.6.0
gotchaparseStmt by default rejects trailing content (e.g., multiple semicolon-separated statements). This differs from parse which accepts multiple statements.
fix
Use parse for multi-statement scripts, or pass allowTrailing: true to parseStmt for incremental parsing.
affects: >=0.6.0
breakingIn v0.6.0, the return type changed from {ast} to {root} and added status field. Old code using result.ast will break.
fix
Access result.root instead of result.ast, and check result.status before accessing root.
affects: <0.6.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: Must use import to load ES Module: /node_modules/sqlite3-parser/index.js require() of ES modules is not supported.
Trying to require() the package which is ESM-only since v0.7.0.
fix
Replace require('sqlite3-parser') with import { parse } from 'sqlite3-parser'.
TypeError: result.ast is undefined
Accessing result.ast in code written for pre-0.6.0 API.
fix
Use result.root instead of result.ast, and check result.status === 'ok' first.
ParseError: unexpected token at position X
Input SQL contains syntax errors or unsupported SQLite features.
fix
Check error message and location. Use parseStmt with allowTrailing to isolate problematic statements.
Upgrade
Version history
0.7.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Meta
2
OpenAI (training)
1
Resources
sqlite3-parser — npm install sqlite3-parser · libregistry