Registry / devops / prettier-plugin-pegjs

prettier-plugin-pegjs

JSON →
library2.0.2jsnpmunverified

A Prettier plugin for formatting PEG.js and Peggy grammar files. v2.0.2 is the latest stable, requires Node >= 16 and ships TypeScript definitions. It supports standard Prettier options plus an 'actionParser' to format action code (JavaScript/TypeScript by default, configurable). Unlike generic formatters, it deeply understands PEG syntax, preserving code within actions. Available for browser via standalone bundle.

npm install prettier-plugin-pegjs
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-PE
P
prettier-plugin-pegjs
devopsjavascriptv2.0.2
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 prettierPluginPegjs from 'prettier-plugin-pegjs'
const prettierPluginPegjs = require('prettier-plugin-pegjs')
Package is ESM-only. Use dynamic import() for CommonJS projects.
printPrettier
import { printPrettier } from 'prettier-plugin-pegjs/standalone'
import { printPrettier } from 'prettier-plugin-pegjs'
printPrettier is exported from the standalone subpath. Direct import from main entry only exports the Prettier plugin object.
PegjsParser
import { PegjsParser } from 'prettier-plugin-pegjs'
const PegjsParser = require('prettier-plugin-pegjs').PegjsParser
TypeScript type export; only available for type imports in CJS.

Formats a PEG.js grammar string using Prettier with the plugin, demonstrating usage in a Node.js TypeScript environment.

import prettier from 'prettier'; import prettierPluginPegjs from 'prettier-plugin-pegjs'; const code = `Expression = head:Term tail:(_("+"/"-")_ Term)* { return tail.reduce(function(result, element) { if (element[1] === "+") { return result + element[3]; } if (element[1] === "-") { return result - element[3]; } }, head) }`; const formatted = await prettier.format(code, { parser: 'pegjs', plugins: [prettierPluginPegjs], tabWidth: 2, }); console.log(formatted);
Debug
Known issues
gotchaThe plugin parses PEG.js (original) grammar, not Peggy (fork) — ensure your grammar file uses .pegjs extension or specify parser explicitly
fix
Use .pegjs extension or set parser: 'pegjs' in Prettier config
affects: all
breakingv2.0.0 dropped support for Node < 16, CommonJS via require() fails
fix
Upgrade to Node 16+ and use ESM imports (import) or dynamic import()
affects: >=2.0.0
deprecatedUsing default export with require('prettier-plugin-pegjs') will return undefined in older versions — always use default import
fix
Migrate to import syntax or upgrade to v2 (ESM only)
affects: >=1.0.0 <2.0.0
gotchaactionParser option defaults to 'babel-ts' — may cause parse errors if actions contain other languages
fix
Set actionParser to appropriate parser (e.g., 'babel' for JavaScript without TypeScript)
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'prettier-plugin-pegjs'
Plugin not installed or not in node_modules
fix
Run 'npm install --save-dev prettier prettier-plugin-pegjs' or 'yarn add --dev prettier prettier-plugin-pegjs'.
SyntaxError: Unexpected token (1:1) while parsing PEG grammar
The grammar file is not valid PEG.js syntax (e.g., missing header or invalid rule)
fix
Check the grammar against PEG.js specification; ensure initial rule is uppercase.
TypeError: prettier.format is not a function
Using CommonJS require with ESM-only version or importing incorrectly
fix
Use dynamic import: const prettier = await import('prettier'); or migrate to ESM.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
prettierrequiredpeer dependency, plugin requires Prettier to be installed
Agent activity
4 hits · last 30 days
node
4
Resources
prettier-plugin-pegjs — npm install prettier-plugin-pegjs · libregistry