Registry / serialization / pgn-parser

pgn-parser

JSON →
library1.1.0jsnpmunverified

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-parser
INSTALL
IMPORT
SIG · PGN-PARSER
P
pgn-parser
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pgnParser
const pgnParser = require('pgn-parser');
import pgnParser from 'pgn-parser';
This package is CommonJS-only. Attempting to use ES module `import` syntax directly without a transpiler or specific Node.js configuration will result in an error in pure ESM environments.
parse
const { parse } = require('pgn-parser'); // or const pgnParser = require('pgn-parser'); const parse = pgnParser.parse;
import { parse } from 'pgn-parser';
The primary parsing function `parse` is exposed as a method on the default export. Destructuring `parse` directly from `require` is a common pattern in CJS for clarity.

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.

const pgnParser = require('pgn-parser'); const pgnString = ` [Event "Rated Blitz game"] [Site "https://lichess.org/123abcde"] [Date "2024.04.19"] [White "ChessMaster"] [Black "BeginnerBot"] [Result "1-0"] [WhiteElo "1800"] [BlackElo "1200"] [ECO "C42"] [TimeControl "300+0"] [UTCDate "2024.04.19"] [UTCTime "21:00:00"] [Opening "Russian Game: Three Knights Attack"] 1. e4 e5 2. Nf3 Nc6 3. Bb5 Nf6 (3...a6 {is also common}) 4. O-O Nxe4 5. Re1 Nd6 6. Nxe5 Be7 7. Bxc6 dxc6 8. d4 O-O 9. Nc3 Bf5 10. Bf4 Re8 11. h3 Bg5 12. Bg3 Bh4 13. Qd2 Bxg3 14. fxg3 Nb5 15. Nxb5 cxb5 16. Rf1 Be6 17. b3 c5 18. Rad1 cxd4 19. Qxd4 Qg5 20. Kh2 Rac8 21. c4 bxc4 22. Nxc4 Bxc4 23. bxc4 b6 24. Qd7 Rf8 25. Rd5 Qe3 26. Rff5 h6 27. Rde5 Qc3 28. Re7 a5 29. Rfxf7 Rxf7 30. Rxf7 Kh8 31. Re7 Rg8 32. Qg4 Qf6 33. Rb7 Rd8 34. Rd7 Rf8 35. h4 Kh7 36. Rd5 Qf2 37. a4 Re8 38. Rd7 Qf6 39. Rd5 Rf8 40. Rd7 Qf2 41. Rd5 Rf6 42. Rd7 Rg6 43. Qf3 Qxf3 44. gxf3 Rc6 45. Rd4 h5 46. Rd5 Kh6 47. Kg2 Rg6 48. g4 hxg4 49. fxg4 Rc6 50. Rh5+ Kg6 51. Rg5+ Kh6 52. Rd5 Kg6 53. Rd4 Kh6 54. Kf3 Rf6+ 55. Ke4 Rc6 56. Kd5 Rg6 57. Ke5 Rc6 58. Kd5 Rg6 59. Ke5 Rc6 60. Rd6+ Kh7 61. Rxc6 1-0 `; try { const [gameResult] = pgnParser.parse(pgnString); console.log('Parsed Headers:', gameResult.headers); console.log('First Move:', gameResult.moves[0]); console.log('Last Move:', gameResult.moves[gameResult.moves.length - 1]); console.log('Game Result:', gameResult.result); } catch (error) { console.error('Failed to parse PGN:', error.message); }
Debug
Known issues
gotchaThe `pgn-parser` package is designed for CommonJS (CJS) environments and primarily uses `require()` for module loading. Direct `import` statements in native ES module (ESM) contexts will fail unless a bundler or Node.js's `--experimental-json-modules` flag is used for transpilation or compatibility.
fix
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.
affects: >=1.0.0
gotchaThe library's development appears to be in maintenance mode, with the last publish over a year ago. While functional, new features, bug fixes, or compatibility updates for newer JavaScript runtimes or PGN specification changes may not be actively provided. Users seeking more actively developed alternatives might consider other PGN parsing libraries.
fix
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.
affects: >=2.2.1
gotchaPGN files can sometimes be 'dirty' or non-standard, containing extra characters or malformed sections. While `pgn-parser` aims for robust parsing, extremely malformed input might lead to unexpected parsing errors or incomplete data extraction. The parser is built on a strict grammar (PEG.js), which means it might be less forgiving than parsers designed with 'dirty PGN' handling as a primary concern.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: pgnParser.parse is not a function
This typically occurs when the `pgn-parser` module is imported incorrectly, or when attempting to call `parse` on an undefined or improperly destructured object. This can happen if using an `import` statement in a CJS context or when the default export isn't captured correctly.
fix
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).
SyntaxError: Cannot use import statement outside a module
You are attempting to use ES module `import` syntax in a Node.js environment that is configured for CommonJS (the default for `.js` files without `"type": "module"` in `package.json`). `pgn-parser` is a CJS module.
fix
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.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
pgn-parser — npm install pgn-parser · libregistry