The `machine` package is a JavaScript runner for functions that adhere to the Node-Machine specification, an open standard for atomic, context-free subroutines. It allows developers to define functions (called 'machines') with explicit inputs and exits, enabling robust static analysis, automatic documentation generation, UI inference, and advanced toolchain integration. This package, currently at version 15.2.3, provides the core `Machine.build()` method to transform machine definitions into callable JavaScript functions. While it sees continuous development with incremental releases and pre-releases, direct usage is often unnecessary, as higher-level `node-machine` modules (like `machine-as-action` or `machine-as-script`) frequently abstract its functionality. Its key differentiator is enforcing a strict function signature and behavior via the machine spec, which promotes maintainability and composability.
npm install machineVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a simple 'machine' with inputs and exits, building it into a callable function, and executing it using async/await syntax to handle both success and error paths, showcasing input defaults and basic error handling.
Update error handling logic for validation errors to inspect the new error object structure. Specifically, look for a `raw.errors` property containing detailed validation failures instead of relying on a top-level `machine ID`.
Migrate `example` properties within exit definitions to `outputExample` to ensure future compatibility and adherence to the updated specification. `outputExample` explicitly clarifies the property's purpose.
Before integrating `machine` directly, evaluate if a higher-level `node-machine` toolchain module better suits your project's needs, as they often wrap and simplify `machine`'s core functionality.
Upgrade to `machine` v13.0.0-17 or a newer version to benefit from improved performance when executing asynchronous machines.
Ensure you first build a machine definition into a callable function: `const callable = Machine({ /* definition */ });` then invoke `callable(inputs);`.For versions `>=13.0.0`, the validation error object structure has changed. Access validation details through `err.raw.errors` (e.g., `err.raw.errors[0].message`) instead of previous top-level properties.