{"id":15822,"library":"slack-message-parser","title":"Slack Message Parser","description":"slack-message-parser is a JavaScript/TypeScript library designed to parse Slack's rich message formatting syntax into a structured Abstract Syntax Tree (AST). This allows developers to programmatically inspect, transform, or render Slack messages outside of the Slack client itself. The current stable version is 3.0.2. The library maintains a steady release cadence, primarily focusing on bug fixes, improvements to parsing accuracy, and adapting to changes in Node.js environments, with major versions introducing breaking changes like the shift to ESM and higher Node.js version requirements. A key differentiator of slack-message-parser is its output: instead of directly rendering to a format like HTML, it provides a hierarchical AST, enabling highly customizable processing, such as converting to various markup languages, performing content analysis, or implementing custom rendering logic. It ships with comprehensive TypeScript types, ensuring robust and type-safe development.","status":"active","version":"3.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/pocka/slack-message-parser","tags":["javascript","slack","typescript"],"install":[{"cmd":"npm install slack-message-parser","lang":"bash","label":"npm"},{"cmd":"yarn add slack-message-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add slack-message-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v3.0.0, the package is primarily distributed as ES Modules (ESM). Direct CommonJS `require()` for the default export will not correctly import the parsing function.","wrong":"const slackMessageParser = require('slack-message-parser');","symbol":"slackMessageParser","correct":"import slackMessageParser from 'slack-message-parser';"},{"note":"The `Node` interface is a TypeScript type export. For runtime access to named exports in CommonJS after v3.0.0, dynamic `import()` or specific Node.js configuration for ESM interoperability is required.","wrong":"const { Node } = require('slack-message-parser');","symbol":"Node","correct":"import { Node } from 'slack-message-parser';"},{"note":"The `NodeType` enum is a named export. Like other named exports, it is part of the ESM build since v3.0.0. CommonJS users may face issues with direct `require()` calls.","wrong":"const { NodeType } = require('slack-message-parser');","symbol":"NodeType","correct":"import { NodeType } from 'slack-message-parser';"}],"quickstart":{"code":"import slackMessageParser, { Node, NodeType } from \"slack-message-parser\";\n\nconst tree = slackMessageParser(\"Slack *message* ~to~ _parse_\");\n\n// Write your own!\nconst toHTML = (node: Node): string => {\n  switch (node.type) {\n    case NodeType.Root:\n      return `<p>${node.children.map(toHTML).join(\"\")}</p>`;\n    case NodeType.Text:\n      return node.text;\n    case NodeType.Bold:\n      return `<strong>${node.children.map(toHTML).join(\"\")}</strong>`;\n    case NodeType.Italic:\n      return `<i>${node.children.map(toHTML).join(\"\")}</i>`;\n    case NodeType.Strike:\n      return `<del>${node.children.map(toHTML).join(\"\")}</del>`;\n    default:\n      // You can use `source` property, which every nodes have, to serialize unknown nodes as-is\n      return node.source;\n  }\n};\n\nconsole.log(toHTML(tree));\n// Expected Output:\n// '<p>Slack <strong>message</strong> <del>to</del> <i>parse</i></p>'","lang":"typescript","description":"This example demonstrates how to parse a Slack message string into an AST and then recursively convert that AST into an HTML string, illustrating custom rendering."},"warnings":[{"fix":"Ensure your Node.js environment is at least version 16.0.0. Update Node.js if necessary.","message":"Version 3.0.0 introduced a breaking change requiring Node.js version 16 or greater due to the shift in shipped code from ES2015 to ES2021.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Migrate to `import` statements and ensure your project or Node.js environment is configured for ESM. For CJS projects that cannot migrate, consider dynamic `import()` or use a transpiler.","message":"With v3.0.0, the package transitioned to ES Modules (ESM) builds. Projects using CommonJS (CJS) may encounter issues with `require()` statements or need to configure Node.js for ESM interoperability.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Explicitly type the return of `parse` as `Root` or adjust type assertions/guards to correctly handle the `Root` type for the top-level AST structure.","message":"In v2.0.0, the TypeScript typing for the return type of the `parse` function changed from `Node` to `Root`. While `Node` previously encompassed `Root`, this is a breaking change for code that directly inferred or expected `Node` when specifically working with the root of the parsed tree.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Upgrade to slack-message-parser v3.0.2 or newer to ensure accurate parsing of multiline quotes, regardless of whether the `>>>` prefix is used.","message":"Prior to v3.0.2, the parser did not correctly support multiline quotes that omitted the `>>>` prefix, potentially leading to incorrect or incomplete parsing of such message blocks.","severity":"gotcha","affected_versions":"<3.0.2"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured as an ES Module (e.g., by adding `\"type\": \"module\"` to `package.json` or using `.mjs` file extensions), or use dynamic `import()` if you must remain in CJS.","cause":"Attempting to use ES Module `import` syntax in a CommonJS (CJS) project context, particularly after v3.0.0 shifted the library to ESM.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Use the ESM `import` syntax: `import slackMessageParser from 'slack-message-parser';`. If you must use `require()` in a CJS context, try `const { default: slackMessageParser } = require('slack-message-parser');` or ensure Node.js correctly interops.","cause":"Incorrectly attempting to `require()` the default export of `slack-message-parser` in a Node.js environment where the package is treated as an ESM, especially in v3.0.0 and above.","error":"TypeError: slackMessageParser is not a function"},{"fix":"Implement type guards or switch statements based on `node.type` to correctly narrow the `Node` type to `Root` or other specific types (e.g., `NodeType.Bold`, `NodeType.Italic`) before accessing properties like `children`.","cause":"This TypeScript error occurs if you are iterating or accessing `children` directly on a generic `Node` type, especially after the v2.0.0 change, without first narrowing the type to `Root` or other node types that define `children`.","error":"Property 'children' does not exist on type 'Node'."}],"ecosystem":"npm"}