JavaScript state machine libraries

Updated Nov 2026

JavaScript state machine libraries model application logic as a fixed set of states and the events that move between them. A machine accepts only the transitions its definition allows rather than any combination of flags. These libraries keep checkout flows, UI components, and AI agent workflows in a known state at every step.

Popularity

XState FSM has the most monthly downloads in this comparison, at 38.7M.

LibraryMonthly downloadsStarsLast releaseLicense
@xstate/fsm
38.7M
30.5KJun 2023MIT
XState
31.2M
30.5KNov 2026MIT
machina
14.0M
2.0KOct 2026MIT
robot3
9.4M
2.2KOct 2026BSD-2-Clause
@zag-js/core
8.6M
5.3KNov 2026MIT
Finity
4.2M
135Mar 2018MIT
javascript-state-machine
1.1M
8.7KJul 2018MIT
Stately.js
70.4K
814Oct 2017MIT
Downloads: npm, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Runs minimal finite state machines with the XState model.

Downloads / mo
38.7M
Stars
30.5K
Last release
2.1.0
License
MIT

Quickstart

From README.md
$ npm install @xstate/fsm
import { createMachine } from '@xstate/fsm';

const machine = createMachine({
  initial: 'idle',
  states: { idle: { on: { START: 'running' } }, running: {} },
});
const nextState = machine.transition(machine.initialState, 'START');

@xstate/fsm examples

From open-source projects on GitHub

Models application logic with state machines, nested states, and actors.

Downloads / mo
31.2M
Stars
30.5K
Last release
5.33.4
License
MIT

Strengths

  • Groups behavior into nested states. Use parent states to share behavior across related child states.
  • Manages asynchronous actor lifecycles. Invoke actors when entering a state and stop them when leaving.

Limitations

  • Async actions are not awaited. Use invoked actors when the next state depends on asynchronous work.
  • Guards must be synchronous. Finish asynchronous checks before sending the event that tests a guard.

Quickstart

From README.md
$ npm install xstate
import { createMachine, createActor } from 'xstate';

const machine = createMachine({
  initial: 'idle',
  states: { idle: { on: { START: 'running' } }, running: {} },
});
const actor = createActor(machine).start();
actor.send({ type: 'START' });

XState examples

From open-source projects on GitHub

Defines input-driven state machines and shared behaviors for independent client objects.

Downloads / mo
14.0M
Stars
2.0K
Last release
8.2.0
License
MIT

Strengths

  • Shares behavior across client objects. Use createBehavioralFsm to track each client’s state independently.
  • Replays inputs after a transition. Use defer() when an input arrives before the machine can handle it.

Limitations

  • Async work needs completion events. Start asynchronous work separately, then call handle() or transition() when finished.
  • Child handlers take priority. Parent handlers run only when the child chain cannot handle an input.

Quickstart

From README.md
$ npm install machina
import { createFsm } from 'machina';

const machine = createFsm({
  id: 'task', initialState: 'idle', context: {},
  states: { idle: { START: 'running' }, running: {} },
});
machine.handle('START');

Composes immutable state-machine definitions from state, transition, guard, and invocation functions.

Downloads / mo
9.4M
Stars
2.2K
Last release
1.4.0
License
BSD-2-Clause

Strengths

  • Composes nested machines through invocation. Invoke a child machine from a parent state.
  • Routes promise outcomes into events. Handle done and error transitions from invoked promise-returning functions.

Limitations

  • Leaving a state discards results. Results are ignored after leaving their invoking state.

Quickstart

From interpret.md
$ npm install robot3
import { createMachine, interpret, state, transition } from 'robot3';

const machine = createMachine({
  idle: state(transition('START', 'running')),
  running: state(),
});
const service = interpret(machine, () => {});
service.send('START');

robot3 examples

From open-source projects on GitHub

@zag-js/core

Implements a minimal XState FSM for UI machines.

Downloads / mo
8.6M
Stars
5.3K
Last release
1.48.0
License
MIT

Quickstart

From state.d.ts
$ npm install @zag-js/core
import { createMachine, findTransition } from '@zag-js/core';

const machine = createMachine({
  initialState: () => 'idle',
  states: { idle: { on: { START: { target: 'running' } } }, running: {} },
});
const match = findTransition(machine, 'idle', 'START');

Defines finite state machines for Node.js and the browser through a configuration DSL.

Downloads / mo
4.2M
Stars
135
Last release
0.5.4
License
MIT

Quickstart

From README.md
$ npm install finity
import Finity from 'finity';

const machine = Finity.configure()
  .initialState('idle').on('START').transitionTo('running')
  .state('running').start();
machine.handle('START');

Finity examples

From open-source projects on GitHub

javascript-state-machine

Defines named states and transitions with generated transition methods and lifecycle observers.

Downloads / mo
1.1M
Stars
8.7K
Last release
3.1.0
License
MIT

Strengths

  • Observes transition lifecycle events. Attach observers before transitions, during state changes, or after completion.
  • Pauses transitions for promises. Return a promise from a lifecycle observer to delay transition completion.

Limitations

  • Pending transitions reject overlapping triggers. Wait for a pending transition or handle onPendingTransition explicitly.
  • Unsettled promises block the machine. Resolve or reject every promise returned from a transition observer.

Quickstart

From README.md
$ npm install javascript-state-machine
const StateMachine = require('javascript-state-machine');

const machine = new StateMachine({
  init: 'idle',
  transitions: [{ name: 'start', from: 'idle', to: 'running' }],
});
machine.start();

javascript-state-machine examples

From open-source projects on GitHub

Runs finite state machines in Node.js and the browser.

Downloads / mo
70.4K
Stars
814
Last release
2.0.0
License
MIT

Quickstart

From README.md
$ npm install stately.js
const Stately = require('stately.js');

const machine = new Stately({
  idle: { START: 'running' },
  running: {},
}, 'idle');
machine.START();

Stately.js examples

From open-source projects on GitHub
var detachHandler = function(frame) {
  if (this.getMachineState() === 'ATTACHED') {
    this.ATTACHED.sendDetach({ closed: frame.closed });
  }

  link._detached(frame);
  return this.DETACHED;
};

FAQ

XState suits apps that need nested and parallel states with an actor model. XState FSM suits apps that need a minimal flat state machine. Robot suits code that prefers functional, immutable machine definitions.

A state machine is a model with a finite set of states, where each event moves the system from one state to another by a defined transition. Only one state is active at a time in a flat machine.

Statecharts extend state machines with nested states, parallel regions, and history. XState implements full statecharts. XState FSM leaves those features out.

XState models application logic with state machines, statecharts, and actors. It supports nested and parallel state configurations.

Zag core is the state machine core behind the Zag UI component machines. It implements a minimal version of the XState FSM model for UI components.

For coding agents

Found your state machine library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official