{"id":14931,"library":"sql-parser","title":"SQL Parser","description":"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.","status":"abandoned","version":"0.5.0","language":"javascript","source_language":"en","source_url":"http://github.com/forward/sql-parser","tags":["javascript"],"install":[{"cmd":"npm install sql-parser","lang":"bash","label":"npm"},{"cmd":"yarn add sql-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is CommonJS-only and does not support ESM imports.","wrong":"import { lexer } from 'sql-parser';","symbol":"lexer","correct":"const { lexer } = require('sql-parser');"},{"note":"The parser module is exported as a named property from the main CommonJS export.","wrong":"import parser from 'sql-parser';","symbol":"parser","correct":"const { parser } = require('sql-parser');"},{"note":"Accessing the Lexer constructor directly via `require('sql-parser').Lexer` might also be possible, depending on internal structure.","wrong":"import { Lexer } from 'sql-parser';","symbol":"Lexer","correct":"const Lexer = require('sql-parser').Lexer;"}],"quickstart":{"code":"const { lexer, parser } = require('sql-parser');\n\nconst sqlQuery = \"select id, name from users where age > 18 order by name asc limit 10\";\n\nconsole.log('Original SQL:', sqlQuery);\n\n// Tokenize the SQL query\nconst tokens = lexer.tokenize(sqlQuery);\nconsole.log('Tokens:', JSON.stringify(tokens, null, 2));\n\n// Parse the tokens into a Select object (AST-like structure)\ntry {\n  const selectObject = parser.parse(tokens);\n  console.log('Parsed Object (simplified):', selectObject);\n\n  // Convert the parsed object back to a formatted SQL string\n  const formattedSql = selectObject.toString();\n  console.log('Formatted SQL from object:\\n', formattedSql);\n} catch (e) {\n  console.error('Parsing error:', e.message);\n}","lang":"javascript","description":"Demonstrates tokenizing a basic SELECT query, parsing it into a 'Select' object, and then re-generating a formatted SQL string."},"warnings":[{"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.","message":"This 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).","severity":"breaking","affected_versions":">=0.1.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"<=0.5.0"},{"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()`.","message":"This 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.","severity":"gotcha","affected_versions":"<=0.5.0"},{"fix":"Manual type declarations (`.d.ts` files) would be required to leverage TypeScript's benefits. For typed SQL parsers, explore more modern alternatives.","message":"The 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`.","severity":"gotcha","affected_versions":"<=0.5.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `const { lexer } = require('sql-parser');` or `const sqlParser = require('sql-parser'); const lexer = sqlParser.lexer;`.","cause":"The `lexer` object was not correctly destructured or assigned from the `require('sql-parser')` call, or the package structure differs from expectation.","error":"TypeError: lexer.tokenize is not a function"},{"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.","cause":"The SQL query contains syntax or constructs not supported by this basic parser (e.g., complex joins, subqueries, DDL statements, specific SQL dialect features).","error":"Error: Parse error on line X column Y: Unexpected token"},{"fix":"Change the import statement to CommonJS `require()`: `const { lexer } = require('sql-parser');`.","cause":"Attempting to use ESM `import` syntax (`import { lexer } from 'sql-parser';`) for a CommonJS-only package.","error":"SyntaxError: Named export 'lexer' not found. The requested module 'sql-parser' does not provide an export named 'lexer'"}],"ecosystem":"npm"}