{"id":22405,"library":"sqlite3-parser","title":"sqlite3-parser","description":"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).","status":"active","version":"0.7.1","language":"javascript","source_language":"en","source_url":"https://github.com/justjake/sqlite3-parser-js","tags":["javascript","sql","sqlite","parser","ast","lalr","syntax","tokenizer","sqlite3","typescript"],"install":[{"cmd":"npm install sqlite3-parser","lang":"bash","label":"npm"},{"cmd":"yarn add sqlite3-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add sqlite3-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only since v0.7.0; CommonJS require is not supported. Use dynamic import or bundler with ESM interop.","wrong":"const parse = require('sqlite3-parser').parse","symbol":"parse","correct":"import { parse } from 'sqlite3-parser'"},{"note":"Same ESM-only constraint. TypeScript users must ensure moduleResolution is 'node16' or 'bundler'.","wrong":"const { parseStmt } = require('sqlite3-parser')","symbol":"parseStmt","correct":"import { parseStmt } from 'sqlite3-parser'"},{"note":"Convenience function that throws on parse error. Useful when you know the SQL is valid.","symbol":"parseOrThrow","correct":"import { parseOrThrow } from 'sqlite3-parser'"},{"note":"ParseOk is a type-only export. Use `import type` to avoid runtime errors in isolated modules.","wrong":"import { ParseOk } from 'sqlite3-parser'","symbol":"type ParseOk","correct":"import type { ParseOk } from 'sqlite3-parser'"},{"note":"Error diagnostics are not subclasses of Error. Always check `result.status` before accessing error info.","symbol":"type ParseDiagnostic","correct":"import type { ParseDiagnostic } from 'sqlite3-parser'"}],"quickstart":{"code":"import { parse, parseStmt, parseOrThrow } from 'sqlite3-parser';\n\n// Parse multiple statements\nconst multi = parse(`SELECT * FROM t1; INSERT INTO t2 VALUES (1)`);\nif (multi.status === 'ok') {\n  console.log(multi.root.cmds.length); // 2\n  console.log(multi.root.cmds[0].type); // SelectStmt\n}\n\n// Parse single statement\nconst single = parseStmt('SELECT id FROM users WHERE name = ?');\nif (single.status === 'ok') {\n  console.log(single.root.type); // SelectStmt\n}\n\n// Parse or throw\nconst stmt = parseOrThrow('UPDATE t SET x = 1');\nconsole.log(stmt.root.type); // CmdList (always contains one command)\n\n// Incremental parsing with allowTrailing\nconst result = parseStmt('SELECT 1; SELECT 2', { allowTrailing: true });\nif (result.status === 'ok') {\n  console.log(result.tail); // 9 (index where next statement starts)\n}","lang":"typescript","description":"Demonstrates parsing multiple statements, single statement, throwing on error, and incremental parsing with allowTrailing option."},"warnings":[{"fix":"Use import syntax, or upgrade to Node >=14 and use dynamic import.","message":"ESM-only since v0.7.0. Deep require('sqlite3-parser') will fail with ERR_REQUIRE_ESM.","severity":"breaking","affected_versions":">=0.7.0"},{"fix":"Always check result.status === 'ok' before using result.root. Use parseOrThrow for exception-based flow.","message":"ParseDiagnostic is not a subclass of Error; catching 'instanceof Error' will not catch parse errors.","severity":"gotcha","affected_versions":">=0.6.0"},{"fix":"Use parse for multi-statement scripts, or pass allowTrailing: true to parseStmt for incremental parsing.","message":"parseStmt by default rejects trailing content (e.g., multiple semicolon-separated statements). This differs from parse which accepts multiple statements.","severity":"gotcha","affected_versions":">=0.6.0"},{"fix":"Access result.root instead of result.ast, and check result.status before accessing root.","message":"In v0.6.0, the return type changed from {ast} to {root} and added status field. Old code using result.ast will break.","severity":"breaking","affected_versions":"<0.6.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Replace require('sqlite3-parser') with import { parse } from 'sqlite3-parser'.","cause":"Trying to require() the package which is ESM-only since v0.7.0.","error":"ERR_REQUIRE_ESM: Must use import to load ES Module: /node_modules/sqlite3-parser/index.js require() of ES modules is not supported."},{"fix":"Use result.root instead of result.ast, and check result.status === 'ok' first.","cause":"Accessing result.ast in code written for pre-0.6.0 API.","error":"TypeError: result.ast is undefined"},{"fix":"Check error message and location. Use parseStmt with allowTrailing to isolate problematic statements.","cause":"Input SQL contains syntax errors or unsupported SQLite features.","error":"ParseError: unexpected token at position X"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}