Registry / database / sql-parser

sql-parser

JSON →
library0.5.0jsnpmunverified

The `sql-parser` package provides a JavaScript-based lexer and parser specifically designed for SQL syntax. As of version 0.5.0, it is primarily capable of tokenizing and parsing fairly basic `SELECT` queries, offering functionalities like `WHERE`, `GROUP BY`, `ORDER BY`, and `LIMIT` clause parsing. The lexer outputs tokens in a format compatible with JISON, which the parser then consumes to produce a `Select` object. A notable feature is the ability to reconstruct a well-formatted SQL string from the parsed object via its `.toString()` method. The project appears to be in a largely unmaintained state, with no indication of active development towards comprehensive SQL support or regular releases since its initial limited scope. Its implementation borrowed heavily from CoffeeScript project boilerplate, suggesting an older codebase that predates modern JavaScript module patterns and extensive TypeScript adoption.

npm install sql-parser
INSTALL
IMPORT
SIG · SQL-PARSER
S
sql-parser
databasejavascriptv0.5.0
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

lexer
const { lexer } = require('sql-parser');
import { lexer } from 'sql-parser';
The package is CommonJS-only and does not support ESM imports.
parser
const { parser } = require('sql-parser');
import parser from 'sql-parser';
The parser module is exported as a named property from the main CommonJS export.
Lexer
const Lexer = require('sql-parser').Lexer;
import { Lexer } from 'sql-parser';
Accessing the Lexer constructor directly via `require('sql-parser').Lexer` might also be possible, depending on internal structure.

Demonstrates tokenizing a basic SELECT query, parsing it into a 'Select' object, and then re-generating a formatted SQL string.

const { lexer, parser } = require('sql-parser'); const sqlQuery = "select id, name from users where age > 18 order by name asc limit 10"; console.log('Original SQL:', sqlQuery); // Tokenize the SQL query const tokens = lexer.tokenize(sqlQuery); console.log('Tokens:', JSON.stringify(tokens, null, 2)); // Parse the tokens into a Select object (AST-like structure) try { const selectObject = parser.parse(tokens); console.log('Parsed Object (simplified):', selectObject); // Convert the parsed object back to a formatted SQL string const formattedSql = selectObject.toString(); console.log('Formatted SQL from object:\n', formattedSql); } catch (e) { console.error('Parsing error:', e.message); }
Debug
Known issues
breakingThis package is only capable of parsing fairly basic SELECT queries. It does not support a full SQL grammar, including DDL (CREATE, ALTER, DROP), DML (INSERT, UPDATE, DELETE beyond basic SELECT queries), or complex SQL features (e.g., subqueries, joins beyond basic forms, stored procedures).
fix
Do not use this parser for comprehensive or production-grade SQL parsing. Consider alternatives like `node-sql-parser` or `dt-sql-parser` for broader SQL dialect support.
affects: >=0.1.0
gotchaThe project appears to be abandoned or unmaintained. The last publish on npm was March 17, 2015, and the version is 0.5.0, suggesting no active development or updates for a significant period.
fix
Avoid using in new projects requiring active maintenance, security updates, or ongoing feature development. Be aware of potential unpatched vulnerabilities if used with untrusted inputs.
affects: <=0.5.0
gotchaThis package is CommonJS (CJS) only. Attempting to import it using ECMAScript Modules (ESM) syntax (e.g., `import { lexer } from 'sql-parser';`) will result in module resolution errors in an ESM environment.
fix
Ensure you use `require()` syntax for importing the package: `const { lexer, parser } = require('sql-parser');`. If using TypeScript with ESM, ensure your `tsconfig.json` and `package.json` are configured for CJS output or use dynamic `import()`.
affects: <=0.5.0
gotchaThe package does not ship with TypeScript type definitions, nor are external types available on DefinitelyTyped. This means developers using TypeScript will lose type safety when interacting with `sql-parser`.
fix
Manual type declarations (`.d.ts` files) would be required to leverage TypeScript's benefits. For typed SQL parsers, explore more modern alternatives.
affects: <=0.5.0
Errors
Common errors & fixes
TypeError: lexer.tokenize is not a function
The `lexer` object was not correctly destructured or assigned from the `require('sql-parser')` call, or the package structure differs from expectation.
fix
Ensure you are using `const { lexer } = require('sql-parser');` or `const sqlParser = require('sql-parser'); const lexer = sqlParser.lexer;`.
Error: Parse error on line X column Y: Unexpected token
The SQL query contains syntax or constructs not supported by this basic parser (e.g., complex joins, subqueries, DDL statements, specific SQL dialect features).
fix
Simplify the SQL query to only basic SELECT statements with supported clauses (WHERE, GROUP BY, ORDER BY, LIMIT) or use a more robust SQL parsing library if full SQL support is needed.
SyntaxError: Named export 'lexer' not found. The requested module 'sql-parser' does not provide an export named 'lexer'
Attempting to use ESM `import` syntax (`import { lexer } from 'sql-parser';`) for a CommonJS-only package.
fix
Change the import statement to CommonJS `require()`: `const { lexer } = require('sql-parser');`.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
Meta
2
OpenAI (training)
1
Resources