pgn-parser is a JavaScript library designed to parse Portable Game Notation (PGN) chess files into a structured JavaScript object and validate their syntax. The package, currently at version 2.2.1, was last published about a year ago, suggesting it is in maintenance mode rather than active development. It provides a straightforward API to convert PGN strings into a detailed data structure, including game headers, moves, variations, comments, and Numerical Annotation Glyphs (NAGs). This library focuses specifically on PGN parsing and outputting a rich JSON object, differentiating it from full-featured chess engines or game state management libraries. It is built using PEG.js for grammar definition, allowing robust and precise parsing of the PGN specification.
npm install pgn-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a PGN string into a JavaScript object, extracting headers, moves, and the game result. It highlights the basic usage of the `pgnParser.parse` function and shows the structure of the returned data, including nested move details and annotations.
Use `const pgnParser = require('pgn-parser');` for CJS compatibility. If working in an ESM project, consider using dynamic `import('pgn-parser').then(module => ...)` or configure your build system (e.g., Webpack, Rollup) to handle CJS modules.Evaluate the project's long-term viability for your application. For critical projects, consider forking, contributing, or migrating to a more actively maintained PGN parsing library.
Pre-process PGN strings to clean obvious formatting issues before passing them to the parser. Implement robust error handling around `pgnParser.parse()` calls to catch and manage parsing exceptions. Consult the `pgn-parser` GitHub issues for known parsing quirks.
Ensure you are using `const pgnParser = require('pgn-parser');` and then calling `pgnParser.parse()`. If you tried to destructure, confirm `parse` is a direct export (it's not, it's a method on the default export).Change your import to `const pgnParser = require('pgn-parser');`. If you must use ESM syntax in your project, consider dynamic import: `import('pgn-parser').then(module => { const pgnParser = module; ... });` or ensure your environment correctly transpiles or handles CJS modules within an ESM context.No dependency data recorded yet.