Protagonist is a Node.js wrapper that provides programmatic access to Drafter, the high-performance C++ API Blueprint parser. It enables developers to validate and parse API Blueprint documents, a specialized Markdown dialect for describing web APIs, directly within Node.js applications. The current stable version, 2.3.0, integrates with Drafter 5.1.0, offering features like asynchronous and synchronous parsing and validation with configurable options, such as requiring a blueprint name. While it offers direct integration with the powerful Drafter engine, its reliance on `node-gyp` for compilation of native C++ addons during installation means it requires specific build toolchains (Python 2.7, C++ compiler) on the host system. This differentiates it from the simpler-to-install `drafter-npm` package, which provides pre-built binaries and is often recommended for general use, making Protagonist suitable for environments where direct compilation or specific Drafter version control is preferred.
npm install protagonistVerified import paths — ran on the pinned version, not inferred.
Demonstrates asynchronous parsing of a basic API Blueprint document and logs the structured result, disabling the `requireBlueprintName` option for this example.
Upgrade your Node.js environment to version 12 or higher (e.g., `nvm install 16` and `nvm use 16`).
Ensure Python 2.7 (and not Python 3.x, unless explicitly supported by `node-gyp` in your environment) and necessary C++ build tools are installed. For Windows, use `npm install --global --production windows-build-tools`. For macOS, install Xcode Command Line Tools (`xcode-select --install`). For Linux, install `build-essential`.
Consider switching to `drafter-npm` (`npm install drafter-npm`) if local compilation issues persist or if pre-built binaries are preferred for easier deployment.
Prefer the asynchronous `parse` and `validate` APIs to avoid blocking the event loop and ensure non-blocking I/O operations.
Verify Python 2.7 is installed and configured correctly (check `python --version`). Install necessary build tools for your operating system (e.g., `npm install --global --production windows-build-tools` on Windows, `xcode-select --install` on macOS, or `sudo apt-get install build-essential` on Debian/Ubuntu).
Attempt to reinstall the package (`npm rebuild protagonist` or `npm install protagonist`) after ensuring all `node-gyp` prerequisites are met. If issues persist, consider using `drafter-npm` which provides pre-built binaries.
Ensure you are importing the entire module object and then accessing the method: `const protagonist = require('protagonist'); const parseResult = await protagonist.parse(source);` (for CommonJS) or `import protagonist from 'protagonist'; const parseResult = await protagonist.parse(source);` (for ESM, relying on Node's CJS interop).