Registry / database / js-sql-parser

js-sql-parser

JSON →
library1.6.0jsnpmunverified

A JavaScript library for parsing and stringifying SQL SELECT statements into ASTs, based on MySQL 5.7 SELECT grammar. Version 1.6.0 is current; last updated in 2021 with low release cadence. Supports basic SELECT syntax including joins, subqueries, and placeholders like ${param}. Does not support INSERT, UPDATE, DELETE, or DDL. Provides CommonJS, AMD, and browser builds. Key differentiator: minimal and focused solely on SELECT parsing vs full SQL parsers like sql.js or node-sql-parser.

npm install js-sql-parser
INSTALL
IMPORT
SIG · JS-SQL-PARSER
J
js-sql-parser
databasejavascriptv1.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

parser
const parser = require('js-sql-parser');
const { parse, stringify } = require('js-sql-parser');
Only default export is available; named exports are not supported.
parser
import parser from 'js-sql-parser';
import { parse } from 'js-sql-parser';
ESM default import works, as the package has no named exports.

Parse a SELECT query into an AST, modify it, and stringify back to SQL.

const parser = require('js-sql-parser'); const ast = parser.parse('SELECT id, name FROM users WHERE age > 18'); console.log(JSON.stringify(ast, null, 2)); // Modify AST: change SELECT list to count(*) ast.value.selectItems.value[0].value = { type: 'function', name: 'COUNT', args: [{ type: 'star' }] }; const newSql = parser.stringify(ast); console.log(newSql); // SELECT COUNT(*) FROM users WHERE age > 18
Debug
Known issues
breakingThe 'refrence' property was renamed to 'reference' in v1.2.0 — code accessing AST nodes with 'refrence' will break.
fix
Update code to use 'reference' instead of 'refrence' when accessing AST node properties.
affects: <1.2.0
gotchaPlaceholders (${param}) are treated as literal values, not identifiers. Using placeholders for table/column names will produce incorrect SQL or errors.
fix
Use placeholders only for values (e.g., WHERE id = ${val}), not for schema object names.
affects: >=1.5.0
Errors
Common errors & fixes
Cannot find module 'js-sql-parser'
Package not installed or not resolved correctly.
fix
Run 'npm install js-sql-parser' and ensure node_modules contains it.
TypeError: parser.stringify is not a function
Using a named import (e.g., import { stringify } from 'js-sql-parser') instead of default import.
fix
Use default import: import parser from 'js-sql-parser' then parser.stringify(ast).
Upgrade
Version history
1.6.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
js-sql-parser — npm install js-sql-parser · libregistry