A Node.js library for MongoDB-backed leader election, inspired by redis-leader. Current stable version is 1.6.7, with regular releases. It extends EventEmitter to emit 'elected' and 'revoked' events when leadership status changes. Key differentiators: automatic TTL index handling (since 1.5.0) avoids IndexOptionsConflict on config changes; supports lazy start and polling; minimal dependencies. Compared to alternatives like node-zookeeper-leader or redis-leader, it leverages MongoDB's existing infrastructure.
npm install mongo-leaderNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic setup: connect to MongoDB, create Leader instance with TTL/wait options, listen for events, start leader election, poll status, and graceful shutdown.
Use import { Leader } from 'mongo-leader' instead of require()Ensure you pass db = await client.db('name') to the Leader constructorCall leader.start() explicitly after constructing the Leader instance
const db = client.db('myapp'); const leader = new Leader(db);Set unique leaderId option: new Leader(db, { leaderId: 'instance-1' })Upgrade to version >= 1.5.0 which automatically handles TTL index changes.