{"id":20400,"library":"prettier-plugin-pegjs","title":"prettier-plugin-pegjs","description":"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.","status":"active","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/siefkenj/prettier-plugin-pegjs","tags":["javascript","pegjs","parser","prettier","typescript"],"install":[{"cmd":"npm install prettier-plugin-pegjs","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-plugin-pegjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-plugin-pegjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency, plugin requires Prettier to be installed","package":"prettier","optional":false}],"imports":[{"note":"Package is ESM-only. Use dynamic import() for CommonJS projects.","wrong":"const prettierPluginPegjs = require('prettier-plugin-pegjs')","symbol":"default","correct":"import prettierPluginPegjs from 'prettier-plugin-pegjs'"},{"note":"printPrettier is exported from the standalone subpath. Direct import from main entry only exports the Prettier plugin object.","wrong":"import { printPrettier } from 'prettier-plugin-pegjs'","symbol":"printPrettier","correct":"import { printPrettier } from 'prettier-plugin-pegjs/standalone'"},{"note":"TypeScript type export; only available for type imports in CJS.","wrong":"const PegjsParser = require('prettier-plugin-pegjs').PegjsParser","symbol":"PegjsParser","correct":"import { PegjsParser } from 'prettier-plugin-pegjs'"}],"quickstart":{"code":"import prettier from 'prettier';\nimport prettierPluginPegjs from 'prettier-plugin-pegjs';\n\nconst 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) }`;\n\nconst formatted = await prettier.format(code, {\n  parser: 'pegjs',\n  plugins: [prettierPluginPegjs],\n  tabWidth: 2,\n});\nconsole.log(formatted);","lang":"typescript","description":"Formats a PEG.js grammar string using Prettier with the plugin, demonstrating usage in a Node.js TypeScript environment."},"warnings":[{"fix":"Use .pegjs extension or set parser: 'pegjs' in Prettier config","message":"The plugin parses PEG.js (original) grammar, not Peggy (fork) — ensure your grammar file uses .pegjs extension or specify parser explicitly","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to Node 16+ and use ESM imports (import) or dynamic import()","message":"v2.0.0 dropped support for Node < 16, CommonJS via require() fails","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Migrate to import syntax or upgrade to v2 (ESM only)","message":"Using default export with require('prettier-plugin-pegjs') will return undefined in older versions — always use default import","severity":"deprecated","affected_versions":">=1.0.0 <2.0.0"},{"fix":"Set actionParser to appropriate parser (e.g., 'babel' for JavaScript without TypeScript)","message":"actionParser option defaults to 'babel-ts' — may cause parse errors if actions contain other languages","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install --save-dev prettier prettier-plugin-pegjs' or 'yarn add --dev prettier prettier-plugin-pegjs'.","cause":"Plugin not installed or not in node_modules","error":"Error: Cannot find module 'prettier-plugin-pegjs'"},{"fix":"Check the grammar against PEG.js specification; ensure initial rule is uppercase.","cause":"The grammar file is not valid PEG.js syntax (e.g., missing header or invalid rule)","error":"SyntaxError: Unexpected token (1:1) while parsing PEG grammar"},{"fix":"Use dynamic import: const prettier = await import('prettier'); or migrate to ESM.","cause":"Using CommonJS require with ESM-only version or importing incorrectly","error":"TypeError: prettier.format is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}