{"id":11455,"library":"node-sql-parser","title":"SQL Parser for Node.js","description":"Node.js SQL Parser is a versatile library designed to parse various SQL statements into a structured Abstract Syntax Tree (AST) and then reconstruct the SQL from that AST. It supports a comprehensive range of SQL dialects, including MySQL, PostgreSQL, SQLite, Athena, BigQuery, Snowflake, and TransactSQL, among others. Currently at version 5.4.0, the package maintains an active development pace with frequent minor and patch updates, consistently adding support for new SQL syntax features and improving existing dialect compatibility. Key differentiators include its extensive multi-dialect support, the ability to extract visited table and column lists with associated authority, and its dual functionality for both SQL parsing and generation. It offers both Node.js module and browser UMD bundles, providing flexibility across different environments.","status":"active","version":"5.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/taozhi8833998/node-sql-parser","tags":["javascript","sql","sql-parser","parser","node","nodejs","node-parser","node-sql-parser","ast","typescript"],"install":[{"cmd":"npm install node-sql-parser","lang":"bash","label":"npm"},{"cmd":"yarn add node-sql-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-sql-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For ESM environments or TypeScript projects. The `Parser` class is the primary entry point for parsing and sqlifying operations.","wrong":"const Parser = require('node-sql-parser');","symbol":"Parser","correct":"import { Parser } from 'node-sql-parser';"},{"note":"For CommonJS environments. Destructure the `Parser` class from the module export.","wrong":"import { Parser } from 'node-sql-parser';","symbol":"Parser (CommonJS)","correct":"const { Parser } = require('node-sql-parser');"},{"note":"When using the UMD bundle directly in a browser via a `<script>` tag. The `NodeSQLParser` object is exposed globally on `window`.","symbol":"NodeSQLParser (Browser Global)","correct":"const parser = new NodeSQLParser.Parser();"}],"quickstart":{"code":"import { Parser, AST } from 'node-sql-parser';\n\nconst sqlQuery = \"SELECT id, name FROM users WHERE age > 25 AND city = 'New York';\";\n\n// Initialize the parser, defaulting to MySQL dialect. For other dialects, pass { dialect: 'postgresql' } as an option.\nconst parser = new Parser();\n\ntry {\n  // Parse the SQL query into an Abstract Syntax Tree (AST).\n  const ast: AST[] = parser.astify(sqlQuery) as AST[];\n  console.log('Parsed AST:', JSON.stringify(ast, null, 2));\n\n  // Get the list of tables visited in the SQL statement.\n  const tables = parser.tableList(sqlQuery);\n  console.log('Visited Tables:', tables);\n\n  // Get the list of columns visited in the SQL statement.\n  const columns = parser.columnList(sqlQuery);\n  console.log('Visited Columns:', columns);\n\n  // Convert the AST back into a SQL string.\n  const reconstructedSql = parser.sqlify(ast);\n  console.log('Reconstructed SQL:', reconstructedSql);\n} catch (error: any) {\n  console.error('SQL Parsing Error:', error.message);\n}","lang":"typescript","description":"This quickstart demonstrates how to initialize the SQL parser, parse a sample SQL query into an AST, extract table and column lists, and then reconstruct the SQL from the AST. It highlights the basic workflow for SQL analysis and manipulation."},"warnings":[{"fix":"Always pass the appropriate dialect option to the `Parser` constructor, e.g., `new Parser({ dialect: 'postgresql' })`. Refer to the documentation for a list of supported dialects.","message":"The `Parser` constructor defaults to the MySQL dialect if no `dialect` option is explicitly provided. Parsing SQL from other database systems (e.g., PostgreSQL, SQLite) without specifying the correct dialect will likely result in syntax errors or incorrect AST generation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For browser-based applications, consider using dialect-specific UMD bundles if your application only requires a subset of SQL parsing capabilities to reduce overall page load size.","message":"When using `node-sql-parser` in a browser environment via UMD bundles, importing the full `index.umd.js` can result in a large bundle size (approximately 750KB). If you only need support for a specific database, specialized UMD bundles like `mysql.umd.js` or `postgresql.umd.js` are available and significantly smaller (around 150KB).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"When processing the output of `parser.astify()`, be prepared to handle an array of AST objects, even if you anticipate a single statement. Iterate through the array or access the first element if you are certain only one statement is present.","message":"The `astify` method returns an array of AST nodes if multiple SQL statements are separated by semicolons in the input string. If you expect a single statement, always consider that the output might be an array, or ensure your input SQL contains only one statement.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, use `const { Parser } = require('node-sql-parser');`. For ESM/TypeScript, use `import { Parser } from 'node-sql-parser';`. In a browser environment using UMD, ensure the correct script is loaded and access `new NodeSQLParser.Parser()`.","cause":"This typically occurs when trying to `require` the module in a CommonJS context while expecting a default export, or attempting to use browser global `NodeSQLParser.Parser` in a Node.js environment, or an incorrect named import in ESM.","error":"TypeError: Parser is not a constructor"},{"fix":"Review the documentation for a list of supported SQL dialects and ensure the dialect string passed to the options object (e.g., `{ dialect: 'postgresql' }`) is spelled correctly and is indeed supported.","cause":"The `Parser` constructor or `astify`/`sqlify` methods were called with a `dialect` option that is not recognized or supported by the library.","error":"Error: Unknown dialect: 'xyz'"},{"fix":"First, verify the SQL query for correctness against the specified database's syntax. Second, ensure you have correctly set the `dialect` option in the `Parser` constructor to match your SQL. If the SQL is valid for the dialect but still errors, it might be a limitation of the parser itself; check the GitHub issues for similar reports.","cause":"The provided SQL query is either malformed, contains syntax unsupported by the chosen SQL dialect, or contains syntax that the parser currently does not support.","error":"Syntax error near '...' at line X column Y"}],"ecosystem":"npm"}