Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

♟️ Stockfish 17.1 WASM chess engine for Node.js with UCI and analysis.

License

Notifications You must be signed in to change notification settings

shahradelahi/node-stockfish

Repository files navigation

Stockfish

Stockfish for Node.JS

CI NPM Version GPL-3.0 License npm bundle size Install Size

@se-oss/stockfish is a high-performance, TypeScript-first Node.js wrapper for the Stockfish 17.1 WASM chess engine. It provides a convenient API for integrating powerful chess analysis capabilities into your applications, offering functionalities such as position analysis, best move calculation, and direct UCI command interaction.


📦 Installation

npm i @se-oss/stockfish
Install using your favorite package manager

pnpm

pnpm install @se-oss/stockfish

yarn

yarn add @se-oss/stockfish

📖 Usage

Basic Analysis

import { Stockfish } from '@se-oss/stockfish';

const engine = new Stockfish();
await engine.waitReady();

const analysis = await engine.analyze(
  'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
  15
);

console.log('Best move:', analysis.bestmove); // e2e4
console.log('Score:', analysis.lines[0].score); // { type: 'cp', value: 39 }

Advanced UCI Commands

const engine = new Stockfish();

engine.on('info', (info) => {
  console.log(`Depth ${info.depth}: ${info.pv}`);
});

await engine.send('uci');
await engine.send('isready');
await engine.send('position startpos moves e2e4');
await engine.send('go depth 20');

Using the Stockfish Pool

import { StockfishPool } from '@se-oss/stockfish';

const pool = new StockfishPool(4); // Create a pool with 4 engine instances
await pool.initialize();

// Acquire an engine, use it, and release it back to the pool
const engine = await pool.acquire();
console.log('Engine acquired');
const analysis = await engine.analyze(
  'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
  10
);
console.log('Best move from pool engine:', analysis.bestmove);
pool.release(engine);
console.log('Engine released');

// Terminate the pool when done
pool.terminate();

📚 Documentation

For detailed API documentation on all methods, please see the API docs.

🤝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.

License

GPL-3.0 © Shahrad Elahi and contributors.