npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@echecs/pgn

v3.4.0

Published

PGN is a parser that is part of the ECHECS project, designed to interpret the PGN (Portable Game Notation) specification.

Downloads

172

Readme

PGN

npm Test Coverage License: MIT

PGN is a fast TypeScript parser for Portable Game Notation — the standard format for recording chess games.

It parses PGN input into structured move objects with decomposed SAN, paired white/black moves, and full support for annotations and variations. Zero runtime dependencies.

Why this library?

Most PGN parsers on npm either give you raw strings with no structure, or fail on anything beyond a plain game record. If you're building a chess engine, opening book, or game viewer, you need more:

  • Decomposed SAN — every move is parsed into piece, from, to, capture, promotion, check, and checkmate fields. No regex on your side.
  • Paired move structure — moves are returned as [moveNumber, whiteMove, blackMove] tuples, ready to render or process without further work.
  • RAV support — recursive annotation variations ((...) sub-lines) are parsed into a variants tree on each move. Essential for opening books and annotated games.
  • NAG support — symbolic (!, ?, !!, ??, !?, ?!) and numeric ($1$255) annotations are surfaced as an annotations array. Essential for Lichess and ChessBase exports.
  • Multi-game files — parse entire PGN databases in one call. Tested on files with 3 500+ games.
  • Fast — built on a Peggy PEG parser. Throughput is within 1.1–1.2x of the fastest parsers on npm, which do far less work per move (see BENCHMARK_RESULTS.md).

If you only need raw SAN strings and a flat move list, any PGN parser will do. If you need structured, engine-ready output with annotations and variations, this is the one.

Installation

npm install @echecs/pgn

Quick Start

import parse from '@echecs/pgn';

const games = parse(`
  [Event "Example"]
  [White "Player1"]
  [Black "Player2"]
  [Result "1-0"]

  1. e4 e5 2. Nf3 Nc6 3. Bb5 1-0
`);

console.log(games[0].moves[0]);
// [1, { piece: 'P', to: 'e4' }, { piece: 'P', to: 'e5' }]

Usage

parse() takes a PGN string and returns an array of game objects — one per game in the file.

parse(input: string): PGN[]

PGN object

{
  meta:   Meta,   // tag pairs (Event, Site, Date, White, Black, …)
  moves:  Moves,  // paired move list
  result: 1 | 0 | 0.5 | '?'
}

Move object

{
  piece:       'P' | 'R' | 'N' | 'B' | 'Q' | 'K', // always present
  to:          string,       // destination square, e.g. "e4"
  from?:       string,       // disambiguation, e.g. "e" or "e2"
  capture?:    true,
  castling?:   true,
  check?:      true,
  checkmate?:  true,
  promotion?:  'R' | 'N' | 'B' | 'Q',
  annotations?: string[],   // e.g. ["!", "$14"]
  comment?:    string,
  variants?:   Moves[],     // recursive annotation variations
}

Moves are grouped into tuples: [moveNumber, whiteMove, blackMove]. If the last move of a game or variation was made by white, blackMove is undefined.

Annotations and comments

12. Nf3! $14 { White has a slight advantage }
{
  piece: 'N', to: 'f3',
  annotations: ['!', '$14'],
  comment: 'White has a slight advantage'
}

Variations

5... Ba5 (5... Be7 6. d4) 6. Qb3

The alternative line appears as a variants array on the move where it branches:

{
  piece: 'B', to: 'a5',
  variants: [
    [ [5, undefined, { piece: 'B', to: 'e7' }], [6, { piece: 'P', to: 'd4' }] ]
  ]
}

Contributing

Contributions are welcome. Please read CONTRIBUTING.md for guidelines on how to submit issues and pull requests.