{"id":20471,"library":"pretty-format-ast","title":"pretty-format-ast","description":"A plugin for pretty-format2 that prints Abstract Syntax Trees (ASTs) in a human-readable, indented format. Version 1.0.1 is the current stable release. It provides a structured view of AST nodes with their properties, types, and positions. Compared to alternatives like util.inspect, it produces a much more concise and visually organized output, specifically designed for AST inspection. The package works with any AST that follows a similar node structure and requires pretty-format2 as a peer dependency.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/pretty-format/pretty-format-ast","tags":["javascript"],"install":[{"cmd":"npm install pretty-format-ast","lang":"bash","label":"npm"},{"cmd":"yarn add pretty-format-ast","lang":"bash","label":"yarn"},{"cmd":"pnpm add pretty-format-ast","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency; pretty-format-ast is a plugin for pretty-format2 and cannot work without it.","package":"pretty-format2","optional":false}],"imports":[{"note":"CommonJS require works if using CJS, but ESM import is preferred for consistency.","wrong":"const prettyFormatAST = require('pretty-format-ast')","symbol":"default","correct":"import prettyFormatAST from 'pretty-format-ast'"},{"note":"prettyFormat2 is a default export; named exports like 'format' are not available.","wrong":"const { format } = require('pretty-format2')","symbol":"prettyFormat2","correct":"import prettyFormat2 from 'pretty-format2'"},{"note":"The option key is 'plugins' (plural) and must be an array.","wrong":"prettyFormat2(val, { plugin: prettyFormatAST })","symbol":"plugins","correct":"prettyFormat2(val, { plugins: [prettyFormatAST] })"}],"quickstart":{"code":"const prettyFormat2 = require('pretty-format2');\nconst prettyFormatAST = require('pretty-format-ast');\n\nconst ast = {\n  type: 'Program',\n  body: [\n    {\n      type: 'ExpressionStatement',\n      expression: { type: 'Literal', value: 42 },\n    },\n  ],\n};\n\nconsole.log(prettyFormat2(ast, { plugins: [prettyFormatAST] }));\n// Output:\n// Node \"Program\"\n//   body: Array [\n//     Node \"ExpressionStatement\"\n//       expression: Node \"Literal\"\n//         value: 42,\n//   ]","lang":"javascript","description":"Demonstrates basic usage: import pretty-format2 and pretty-format-ast, then format a simple AST object with the plugin."},"warnings":[{"fix":"Use { plugins: [prettyFormatAST] } instead of { plugins: prettyFormatAST }.","message":"Plugin must be passed as an array to the 'plugins' option. Passing a single plugin object without wrapping in an array will be silently ignored.","severity":"gotcha","affected_versions":"<=1.0.1"},{"fix":"Check compatibility with pretty-format2 v2.0.0 before upgrading.","message":"pretty-format2 v1.x is still used, but there is a newer major version v2.x. The plugin may not be compatible with v2.x without updates.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Wait for an updated version of pretty-format-ast or pin pretty-format2 to v1.x.","message":"Starting from pretty-format2 v2.0.0, the plugin API changed. pretty-format-ast v1.0.1 is not updated and may not work with v2.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your AST objects have a 'type' string field for proper formatting.","message":"The plugin only works with AST nodes that have a 'type' property. Objects without 'type' will not be formatted specially.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use import: import prettyFormat2 from 'pretty-format2'; or ensure you have v1.x installed.","cause":"CommonJS require of pretty-format2 may return different export depending on version. In v1.x, it is a default export function, but in v2.x, it may be exported differently.","error":"TypeError: prettyFormat2(...) is not a function"},{"fix":"Run: npm install pretty-format2","cause":"pretty-format2 is a peer dependency and must be installed separately.","error":"Error: Cannot find module 'pretty-format2'"},{"fix":"Use require syntax, or set \"type\": \"module\" in package.json.","cause":"Using ESM imports in a CommonJS script or without proper package.json type: 'module'.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Wrap the plugin in an array: plugins: [prettyFormatAST]","cause":"Passing a non-array value to the 'plugins' option, such as an object.","error":"TypeError: plugins is not iterable"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}