npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@libpg-query/v13

v13.5.7

Published

The real PostgreSQL query parser

Readme

libpg-query

The Real PostgreSQL Parser for JavaScript

Bring the power of PostgreSQL’s native parser to your JavaScript projects — no native builds, no platform headaches.

This is the official PostgreSQL parser, compiled to WebAssembly (WASM) for seamless, cross-platform compatibility. Use it in Node.js or the browser, on Linux, Windows, or anywhere JavaScript runs.

Built to power pgsql-parser, this library delivers full fidelity with the Postgres C codebase — no rewrites, no shortcuts.

Features

  • 🔧 Powered by PostgreSQL – Uses the official Postgres C parser compiled to WebAssembly
  • 🖥️ Cross-Platform – Runs smoothly on macOS, Linux, and Windows
  • 🌐 Node.js & Browser Support – Consistent behavior in any JS environment
  • 📦 No Native Builds Required – No compilation, no system-specific dependencies
  • 🧠 Spec-Accurate Parsing – Produces faithful, standards-compliant ASTs
  • 🚀 Production-Grade – Millions of downloads and trusted by countless projects and top teams

🚀 For Round-trip Codegen

🎯 Want to parse + deparse (full round trip)?
We highly recommend using pgsql-parser which leverages a pure TypeScript deparser that has been battle-tested against 23,000+ SQL statements and is built on top of libpg-query.

Installation

npm install libpg-query

Example

import { parse } from 'libpg-query';

const result = await parse('SELECT * FROM users WHERE active = true');
// {"version":170004,"stmts":[{"stmt":{"SelectStmt":{"targetList":[{"ResTarget" ... "op":"SETOP_NONE"}}}]}

Versions

Our latest is built with 17-latest branch from libpg_query

| PG Major Version | libpg_query | npm dist-tag |--------------------------|-------------|---------| | 17 | 17-6.1.0 | pg17 | 16 | 16-5.2.0 | pg16 | 15 | 15-4.2.4 | pg15 | 14 | 14-3.0.0 | pg14 | 13 | 13-2.2.0 | pg13

Usage

parse(query: string): Promise<ParseResult>

Parses the SQL and returns a Promise for the parse tree. May reject with a parse error.

import { parse } from 'libpg-query';

const result = await parse('SELECT * FROM users WHERE active = true');
// Returns: ParseResult - parsed query object

parseSync(query: string): ParseResult

Synchronous version that returns the parse tree directly. May throw a parse error.

import { parseSync } from 'libpg-query';

const result = parseSync('SELECT * FROM users WHERE active = true');
// Returns: ParseResult - parsed query object

Note: If you need additional functionality like fingerprint, scan, deparse, or normalize, check out the full package (@libpg-query/parser) in the ./full folder of the repo.

Initialization

The library provides both async and sync methods. Async methods handle initialization automatically, while sync methods require explicit initialization.

Async Methods (Recommended)

Async methods handle initialization automatically and are always safe to use:

import { parse } from 'libpg-query';

// These handle initialization automatically
const result = await parse('SELECT * FROM users');

Sync Methods

Sync methods require explicit initialization using loadModule():

import { loadModule, parseSync } from 'libpg-query';

// Initialize first
await loadModule();

// Now safe to use sync methods
const result = parseSync('SELECT * FROM users');

loadModule(): Promise<void>

Explicitly initializes the WASM module. Required before using any sync methods.

import { loadModule, parseSync } from 'libpg-query';

// Initialize before using sync methods
await loadModule();
const result = parseSync('SELECT * FROM users');

Note: We recommend using async methods as they handle initialization automatically. Use sync methods only when necessary, and always call loadModule() first.

Type Definitions

interface ParseResult {
  version: number;
  stmts: Statement[];
}

interface Statement {
  stmt_type: string;
  stmt_len: number;
  stmt_location: number;
  query: string;
}

Note: The return value is an array, as multiple queries may be provided in a single string (semicolon-delimited, as PostgreSQL expects).

Build Instructions

This package uses a WASM-only build system for true cross-platform compatibility without native compilation dependencies.

Prerequisites

  • Node.js (version 16 or higher recommended)
  • pnpm (v8+ recommended)

Building WASM Artifacts

  1. Install dependencies:

    pnpm install
  2. Build WASM artifacts:

    pnpm run build
  3. Clean WASM build (if needed):

    pnpm run clean
  4. Rebuild WASM artifacts from scratch:

    pnpm run clean && pnpm run build

Build Process Details

The WASM build process:

  • Uses Emscripten SDK for compilation
  • Compiles C wrapper code to WebAssembly
  • Generates wasm/libpg-query.js and wasm/libpg-query.wasm files
  • No native compilation or node-gyp dependencies required

Testing

Running Tests

pnpm run test

Test Requirements

  • WASM artifacts must be built before running tests
  • If tests fail with "fetch failed" errors, rebuild WASM artifacts:
    pnpm run clean && pnpm run build && pnpm run test

Troubleshooting

Common Issues

"fetch failed" errors during tests:

  • This indicates stale or missing WASM artifacts
  • Solution: pnpm run clean && pnpm run build

"WASM module not initialized" errors:

  • Ensure you call an async method first to initialize the WASM module
  • Or use the async versions of methods which handle initialization automatically

Build environment issues:

  • Ensure Emscripten SDK is properly installed and configured
  • Check that all required build dependencies are available

Build Artifacts

The build process generates these files:

  • wasm/libpg-query.js - Emscripten-generated JavaScript loader
  • wasm/libpg-query.wasm - WebAssembly binary
  • wasm/index.js - ES module exports
  • wasm/index.cjs - CommonJS exports with sync wrappers

Credits

Built on the excellent work of several contributors:

Related

  • pgsql-parser: The real PostgreSQL parser for Node.js, providing symmetric parsing and deparsing of SQL statements with actual PostgreSQL parser integration.
  • pgsql-deparser: A streamlined tool designed for converting PostgreSQL ASTs back into SQL queries, focusing solely on deparser functionality to complement pgsql-parser.
  • @pgsql/parser: Multi-version PostgreSQL parser with dynamic version selection at runtime, supporting PostgreSQL 15, 16, and 17 in a single package.
  • @pgsql/types: Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs.
  • @pgsql/enums: Provides TypeScript enum definitions for PostgreSQL constants, enabling type-safe usage of PostgreSQL enums and constants in your applications.
  • @pgsql/utils: A comprehensive utility library for PostgreSQL, offering type-safe AST node creation and enum value conversions, simplifying the construction and manipulation of PostgreSQL ASTs.
  • pg-proto-parser: A TypeScript tool that parses PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
  • libpg-query: The real PostgreSQL parser exposed for Node.js, used primarily in pgsql-parser for parsing and deparsing SQL queries.

Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating Software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the Software code or Software CLI, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.