chess.js
Chess move generation, validation and game state in JavaScript.
3. Used by agents' projects
Dependency in 5 public GitHub projects set up for AI coding agents that we scanned (0.1%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What chess.js does
chess.js is a headless chess rules library: it generates and validates moves, reads and writes FEN and PGN, and detects check and game end, which the README sums up as "basically everything but the AI."
chess.js popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How chess.js is used
- chess.js joined the catalog on its public numbers (473,327 downloads a week on npm and 4,461 GitHub stars), not on our project count: it is a dependency in 17 public GitHub projects of our scan, 5 of them set up for AI coding agents.
- Projects that use it most often also use Three.js (41.2%), React Three Fiber (29.4%), React (58.8%).
Often used with
chess.js in practice
Strengths
Out of the box, <code>move()</code> notation is read by a permissive parser. It accepts plain SAN like <code>Nf3</code> and also variants such as <code>g1f3</code>, <code>g1-f3</code> and <code>Ng1xf3</code>.
<code>new Chess(fen)</code> throws if the FEN string is invalid. Setting <code>skipValidation</code> turns that off. The docs use it to load a position with the white king missing.
<code>attackers(square)</code> lists the squares whose pieces can attack a given square. It defaults to the side to move, and an optional color argument switches sides.
<code>board()</code> returns the position as nested arrays of <code>{square, type, color}</code> objects, with <code>null</code> for empty squares. That shape maps directly onto a rendered grid.
Watch out for
The README lists what the library covers and ends with "basically everything but the AI." Its own example game picks moves at random.
The library includes no user interface elements. The docs mention chessboard.js as the library many developers have integrated it with.
The second parser accepts only Standard Algebraic Notation that "strictly adhere[s] to the specification." The docs say it "runs slightly faster but will not parse any non-standard notation."
Play a random game and print the PGN
import { Chess } from 'chess.js'
const chess = new Chess()
while (!chess.isGameOver()) {
const moves = chess.moves()
const move = moves[Math.floor(Math.random() * moves.length)]
chess.move(move)
}
console.log(chess.pgn())More about chess.js
Packages around it
- three: 3D rendering
- @react-three: React Three Fiber packages
- better-sqlite3: SQLite driver
- esbuild: Bundler
- react-router-dom: React routing
- ws: WebSocket library
Alternatives
chess.js This page
Get started
Install it.
npm install chess.jsRules for your agent
## chess.js
- Install with `npm install chess.js`. Import with `import { Chess } from 'chess.js'`, or `const { Chess } = require('chess.js')` in CommonJS.
- chess.js covers "everything but the AI" and has no UI. Do not look in it for move evaluation or rendering code.
- `new Chess(fen)` throws on an invalid FEN, so catch errors when the FEN comes from outside. Pass `{ skipValidation: true }` only when you mean to load an invalid position.
- Drive a game with `isGameOver()`, `moves()` and `move()`. Serialize with `fen()` or `pgn()`.
- The default move parser is permissive. The strict parser accepts only spec-compliant SAN.
- Do not read `getCastlingRights()` as "castling is legal now". The docs say checks and other factors also apply.
- Pass `forceEnpassantSquare` to `fen()` when the output must always include the en passant square.