Skip to main content
Experimental. This can change in a minor release of the CLI, but not in a patch. Pin the CLI version (npx codemod@<version>) if you build on it.
The codemod CLI runs workflows in its own sandboxed JavaScript runtime. To run a workflow inside a Node.js or Bun process instead, use codemod/host. The workflow’s body, function steps and forEachFile callbacks then run in your process, so you can:
  • set breakpoints in them and step through them,
  • measure their coverage,
  • mock the modules they import,
  • start them from your own scripts and tools.
codemod/testing uses this host for TypeScript workflows. See Testing workflows.
This host holds the workflow’s code to the CLI’s rules (no network, commands, writes or timers in callbacks; a replayed clock in the body), so tests fail where the CLI would. It does that by checking Node.js’s own functions while a run’s code runs; it is not a sandbox. Run packages you did not write with the CLI (npx codemod@next <package>).

Run a workflow

codemod/host ships with the codemod package:
scripts/migrate.ts
The first argument is a workflow file or a package directory. In a package directory, the workflow comes from codemod.yaml (pick one by name with workflowName), or is workflow.ts. runWorkflow resolves for every outcome. Check report.status: "succeeded", "failed", "canceled", "paused", "rejected", "awaiting-approval" or "stopped". It rejects only when the workflow cannot be loaded.

Options

Import workflows yourself

Workflows import codemod/workflow, codemod/ast-grep, codemod/runtime and codemod/llm. These are exports of the codemod package, so Node.js, Bun, Vite and Vitest resolve them like any other import, once the package is installed:
Code written for older CLI versions imports the same modules as codemod:workflow, codemod:ast-grep, and so on. The CLI still accepts that spelling. Node.js and Bun need the resolver for it: codemod/host and codemod/testing register it themselves, and anywhere else, import codemod/register first. With Vite and Vitest, add the codemod/vite plugin instead:
vitest.config.ts
You can pass an imported module to runWorkflow, with path set to the workflow file so relative task paths resolve:
A module you import yourself is loaded without the build’s changes, which runWorkflow(path) applies on Node.js as it loads the file. That is fine for most workflows, with two differences: imported forEachFile callbacks are loaded with the workflow and run as plain functions (a step is then identified by the function’s text, not by its file and the files it imports), and shard’s method.function and filter fail with “was not extracted by the build”, as the engine runs them from the build’s modules. For workflows that give shard functions, use runWorkflow(path) on Node.js, or the CLI. Bun loads workflow files without the build’s changes too, even through runWorkflow(path).

How it differs from the CLI

Both hosts run the same orchestrator: step(), replay on resume, until and given work the same way. Tasks the engine owns run in the engine on both: astGrep, shell, shard (with its function and filter, in the engine’s isolates), codemod, agent and the git steps, and so do selecting files for forEachFile and writing what callbacks change. forEachFile callbacks, inline or imported, run in Node.js; codemod/host applies the build’s changes to the workflow file as it loads it, when it loads it by path on Node.js (not for a module you imported yourself, nor on Bun: see Import workflows yourself). A workflow file is checked as the CLI checks it before it runs: a callback that assigns to an outer variable, fs in the workflow body, and the other things the CLI refuses are errors here too, so a workflow that passes its tests also runs with the CLI. codemod/runtime logs and failures behave the same in steps and callbacks on both. On the Node.js host:
  • Pure callbacks are checked as in the CLI. While the workflow’s code runs, in the body or a callback, fetch, XMLHttpRequest, WebSocket, timers (also from node:timers), child_process, fs writes (writeFile, mkdir, rm, rename, open for writing, …) and setting process.env throw the CLI’s errors, and so does importing a built-in module the CLI gives no workflow code (node:http, node:net, node:worker_threads, …), statically or with import(). Callbacks see an empty process.env but keep process.cwd() and process.argv. Reading files is not limited to the target here. Code outside the run, such as your test, is not affected. These checks are not a sandbox: references to host APIs saved before codemod/host loaded can escape them.
  • Semantic analysis (semanticAnalysis) is not available to callbacks on this host: definition() and references() find nothing. Run the workflow with the CLI for it.
  • The workflow body’s clock and randomness are replayed, as in the CLI. In the body, Date, performance.now(), Math.random() and the global crypto.getRandomValues() and crypto.randomUUID() start from the run’s start time and seed, and give the same answers when the run resumes; in callbacks they are real. node:crypto imported in the body is not seeded.
  • codemod/llm is not available yet.
  • codemod(...) steps run local packages only; run workflows that use registry packages with the CLI.
  • definition() and references() find nothing in roots that forEachFile callbacks parse, as in the CLI.
  • Callbacks run one at a time, in this process. The CLI runs forEachFile callbacks on several threads, so a callback that relies on running after another file’s callback works here and not there; return values instead.
  • A step’s timeout cannot stop JavaScript that is running. When a function step or a forEachFile callback runs out of time here, the step fails and its file changes are not written, but its code runs on until it returns; code that never waits is not interrupted at all. The CLI stops them. Tasks such as shell are stopped on both.
  • Events are sent with the CLI. waitForEvent works here for runs kept with store: "local"; send to them with codemod workflow send.
  • The workflow body may wait on a promise no step will settle, which the CLI stops.
  • A run is resumed where it started. Runs started here are resumed with runWorkflow(..., { resume }), and runs the CLI started with codemod workflow resume: the two record steps differently.
  • jssgTransform does not run here, not even in a 1.x transform an upgraded workflow runs with jssgFile from codemod/compat: that transform fails. Other transforms jssgFile runs work as in the CLI. Run the workflow with the CLI for it.
  • YAML workflows do not run here. Run them with the CLI, or convert them with codemod workflow upgrade.
Requires Node.js 20.6 or later, or Bun. Node.js runs TypeScript workflow files directly from version 23.6.