Installation
This guide covers installing XMachines packages in your project across different package managers and environments.
Prerequisites
Before installing XMachines, ensure you have:
- Node.js 22+ (or compatible runtime)
- Package manager: npm, pnpm, or yarn
- TypeScript (optional but recommended for full type safety)
Installing XMachines
Choose your preferred package manager:
npm
npm install @xmachines/play @xmachines/play-xstate @xmachines/play-actor @xmachines/play-signalspnpm
pnpm add @xmachines/play @xmachines/play-xstate @xmachines/play-actor @xmachines/play-signalsyarn
yarn add @xmachines/play @xmachines/play-xstate @xmachines/play-actor @xmachines/play-signalsPackage Selection Guide
XMachines consists of 8 modular packages. Choose the packages based on your needs:
Core Packages (Required)
@xmachines/play
- Foundation package with core protocols and contracts
- Required by all other packages
@xmachines/play-actor
- Abstract actor base class extending XState Actor
- Required for instantiating state machines
@xmachines/play-signals
- TC39 Signals polyfill for reactive updates
- Required for reactive state observation
@xmachines/play-xstate
- XState v5 logic adapter with catalog binding
- Required for state machine definitions
Minimal installation:
npm install @xmachines/play @xmachines/play-xstate @xmachines/play-actor @xmachines/play-signalsUI and Routing (Optional)
@xmachines/play-ui
- UI schema protocol with Zod validation
- Install if: Building declarative UI structures from state
@xmachines/play-router
- Route tree protocol and machine crawler
- Install if: Implementing logic-driven routing
@xmachines/play-tanstack-react-router
- TanStack Router adapter with signal observation
- Install if: Using TanStack Router for navigation
@xmachines/play-react
- React view renderer with JSON-Render
- Install if: Building React applications
@xmachines/play-catalog
- State machine catalog and registry patterns
- Install if: Managing multiple machines with type inference
Complete Installation
For full-featured development with React and routing:
npm install @xmachines/play @xmachines/play-xstate @xmachines/play-actor @xmachines/play-signals @xmachines/play-ui @xmachines/play-router @xmachines/play-tanstack-react-router @xmachines/play-react @xmachines/play-catalogSee also: API Reference for detailed descriptions of each package.
Environment-Specific Setup
Browser (Vite, Webpack, or other bundlers)
No special configuration needed. ESM imports work out of the box:
import { createMachine } from "@xmachines/play-xstate";import { createActor } from "@xmachines/play-actor";import { signal } from "@xmachines/play-signals";Most modern bundlers (Vite, Webpack 5+, Rollup) handle ESM packages automatically.
Node.js
XMachines uses ESM (ECMAScript Modules) exclusively. Ensure your project is configured for ESM:
Option 1: Set "type": "module" in package.json
{ "type": "module", "dependencies": { "@xmachines/play": "^1.0.0" }}Option 2: Use .mjs file extensions
Alternatively, name your files with .mjs extension:
node my-app.mjsImport example:
import { createMachine } from "@xmachines/play-xstate";import { createActor } from "@xmachines/play-actor";
const machine = createMachine({ id: "example", initial: "idle", states: { idle: { on: { START: "running" } }, running: { on: { STOP: "idle" } }, },});
const actor = createActor(machine);actor.start();Deno
XMachines works with Deno via npm specifiers:
import { createMachine } from "npm:@xmachines/play-xstate";import { createActor } from "npm:@xmachines/play-actor";import { signal } from "npm:@xmachines/play-signals";Alternatively, use a CDN like esm.sh:
import { createMachine } from "https://esm.sh/@xmachines/play-xstate";TypeScript
XMachines is written in TypeScript and includes built-in type definitions. No separate @types package is needed.
Recommended tsconfig.json settings:
{ "compilerOptions": { "target": "ESNext", "module": "ESNext", "moduleResolution": "bundler", "strict": true, "esModuleInterop": true, "skipLibCheck": false, "resolveJsonModule": true }}Key settings:
moduleResolution: "bundler"(recommended) or"node16"for Node.js projectsstrict: trueenables full type checkingtarget: "ESNext"leverages modern JavaScript features
Type inference:
XMachines provides full type inference from machine catalog through to components:
import { createMachine } from "@xmachines/play-xstate";
const machine = createMachine({ id: "typed", initial: "idle", context: { count: 0 }, states: { idle: { on: { START: "running" } }, running: { on: { STOP: "idle" } }, },});
// TypeScript infers exact state typestype States = typeof machine.states; // "idle" | "running"Verifying Installation
Test your installation with this simple script:
test.ts (or test.js with "type": "module"):
import { createMachine } from "@xmachines/play-xstate";import { createActor } from "@xmachines/play-actor";
const machine = createMachine({ id: "test", initial: "ready", states: { ready: {}, },});
const actor = createActor(machine);actor.start();
console.log("✓ XMachines installed successfully");console.log("Current state:", actor.getSnapshot().value);Run the test:
# TypeScript (requires tsx or ts-node)npx tsx test.ts
# Or compile firstnpx tsc test.ts && node test.js
# JavaScript (with "type": "module")node test.jsExpected output:
✓ XMachines installed successfullyCurrent state: readyTroubleshooting
Module Not Found
Error: Cannot find module '@xmachines/play-xstate'
Solutions:
- Verify package is installed:
npm list @xmachines/play-xstate - Re-install dependencies:
npm install - Check
node_modules/@xmachines/directory exists
ESM Import Errors
Error: SyntaxError: Cannot use import statement outside a module
Solutions:
- Add
"type": "module"to package.json - Or rename files to
.mjsextension - Or use dynamic import:
const { createMachine } = await import('@xmachines/play-xstate')
Type Errors
Error: Could not find a declaration file for module '@xmachines/play-xstate'
Solutions:
- Types are built-in; no
@typespackage needed - Check TypeScript version (4.5+ required)
- Verify
moduleResolutionin tsconfig.json is set correctly - Try
npm installagain to ensure types are properly linked
Version Mismatches
Error: Type incompatibilities between packages
Solution: Install all XMachines packages with matching major versions:
npm install @xmachines/play@latest @xmachines/play-xstate@latest @xmachines/play-actor@latest @xmachines/play-signals@latestNext Steps
Now that XMachines is installed, you’re ready to build your first state machine:
- Getting Started → - Build your first state machine
- API Reference - Deep dive into each package
- Examples - See XMachines in action
Need help? Check the API Reference for detailed information.