Registry / serialization / prettier-plugin-sql-cst

prettier-plugin-sql-cst

JSON →
library0.19.3jsnpmunverified

A Prettier plugin for SQL that uses sql-parser-cst and the actual Prettier formatting algorithm (Wadler-Leijen). It adapts formatting based on expression length, sticking to one style with minimal configuration options. The plugin supports multiple SQL dialects: SQLite, BigQuery, PostgreSQL, MySQL, and MariaDB (some experimental). It parses and formats SQL code inside CREATE FUNCTION/PROCEDURE bodies (PostgreSQL). Current stable version is 0.19.3, with semi-regular releases (every few weeks). TypeScript types are shipped. Key differentiators vs other SQL formatters: uses Prettier's algorithmic line splitting for consistency, preserves syntax elements, and formats embedded languages.

npm install prettier-plugin-sql-cst
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-SQ
P
prettier-plugin-sql-cst
serializationjavascriptv0.19.3
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.

default
import prettierPluginSqlCst from 'prettier-plugin-sql-cst'
const prettierPluginSqlCst = require('prettier-plugin-sql-cst')
ESM-only since v0.19.3 – the package is compiled into a single ESM .js file. No default export actually exists; you use the package implicitly via Prettier's plugin resolution. Do not import directly.
sqlite
// In .prettierrc: { "plugins": ["prettier-plugin-sql-cst"], "overrides": [{ "files": ["*.sql"], "options": { "parser": "sqlite" } }] }
import { sqlite } from 'prettier-plugin-sql-cst'
sqlite is a parser name string, not an export. It is used as the parser option in Prettier configuration.
bigquery
// In .prettierrc: { "plugins": ["prettier-plugin-sql-cst"], "overrides": [{ "files": ["*.sql"], "options": { "parser": "bigquery" } }] }
import bigquery from 'prettier-plugin-sql-cst'
bigquery is a parser name string, not an export. Use in parser option.

Shows how to install the plugin, configure it in .prettierrc.json, and format a basic SQL query.

// Install dependencies // npm install --save-dev prettier prettier-plugin-sql-cst // Create a .prettierrc.json file: { "plugins": ["prettier-plugin-sql-cst"], "overrides": [ { "files": ["*.sql"], "options": { "parser": "sqlite" } } ] } // Then create a SQL file (e.g., test.sql): SELECT a, b, c FROM tbl WHERE x > 10; // Run prettier: // npx prettier --write test.sql // Output: SELECT a, b, c FROM tbl WHERE x > 10;
prettier --version
Debug
Known issues
breakingv0.19.0+ - Upgraded sql-parser-cst to 0.39.0, possibly changing AST for some SQL constructs. Formatting may change.
fix
Review formatted output after upgrade. No manual fix available.
affects: >=0.19.0
breakingv0.17.0+ - PostgreSQL builtin data type names now formatted in upper case by default. sqlTypeCase, sqlIdentifierCase, sqlFunctionCase options added.
fix
Add sqlTypeCase: 'lower' and sqlIdentifierCase: 'lower' to config for PostgreSQL if lowercase types are desired.
affects: >=0.17.0
breakingv0.19.3 - Package compiled into a single ESM .js file. Breaking for CommonJS consumers.
fix
Ensure project is ESM, or use a dynamic import: const prettierPluginSqlCst = await import('prettier-plugin-sql-cst'). Then use via Prettier's plugin system, not direct import.
affects: >=0.19.3
gotcha`preserve` case option is incompatible with `sqlCanonicalSyntax: true` – added keywords like AS will always be uppercase.
fix
Set sqlCanonicalSyntax: false if preserving case is important, or accept uppercase keywords.
affects: <0.18.0? (present in release notes)
gotchaPostgreSQL, MySQL, MariaDB parsers are experimental – expect crashes on complex queries.
fix
Use sqlite or bigquery parsers for production; test with your SQL dialect thoroughly.
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'prettier-plugin-sql-cst' require() of ES Module
The plugin is ESM-only since v0.19.3, but CommonJS require() is used.
fix
Use import() or ensure your project is configured as ESM. Alternatively, downgrade to v0.19.2 or earlier. Or use Prettier's CLI which handles plugins internally.
TypeError: prettier-plugin-sql-cst.default is not a function
Direct import of the module and calling it as a function.
fix
Do not import or call directly. Use the plugin via Prettier's plugin configuration in .prettierrc or CLI --plugin option.
Error: No parser could be inferred for file: myfile.sql
Prettier cannot auto-detect SQL dialect; no 'parser' option set.
fix
Set parser in .prettierrc overrides or use --parser flag: prettier --plugin prettier-plugin-sql-cst --parser sqlite myfile.sql
Upgrade
Version history
0.19.3latest on npm
Audit
Dependencies
prettierrequiredpeer dependency – the plugin requires Prettier to be installed
Agent activity
6 hits · last 30 days
node
6
Resources
prettier-plugin-sql-cst — npm install prettier-plugin-sql-cst · libregistry