Skip to content
Commands

kubb generate

The generate command runs the Kubb code-generation pipeline from your kubb.config.ts, with watch mode, dry runs, a CI check, and cli, json, file, and html reporters.

kubb generate turns your spec into code with the plugins in your kubb.config.ts. It is the default command: kubb alone runs it.

Terminal
kubb generate                  # input from the config
kubb generate ./petStore.yaml  # this spec instead

Arguments

ArgumentWhat it does
[input]Path, URL, or git input (git+<repo>#<ref>:<path>) of a Swagger or OpenAPI document. Overrides the config's input.

Options

FlagAliasDefaultWhat it does
--config <path>-cPath to a config file, such as ./kubb.staging.ts.
--log-level <level>-linfosilent, info, or verbose. verbose shows plugin timings.
--silent-sfalseSame as --log-level silent.
--verbosefalseSame as --log-level verbose.
--reporter <names>cliComma-separated reporters to run: cli, json, file, html.
--watch-wfalseGenerate again when the local input file changes.
--dry-runfalsePreview the run. Writes no files and skips formatting, linting, and output.postGenerate.
--checkfalseCompare the output with the files on disk and write nothing. Exits with 1 when a file is out of date. See Check mode.
--difffalsePrint a diff for each out-of-date file. Turns --check on.
--forcefalseGenerate, format, and lint even when the run is up to date.
--no-lockfilefalseGenerate without reading or writing kubb-lock.json.

Watch mode

--watch watches only a local input file. A URL or git input generates once, so run Kubb again when the remote spec changes. In an interactive terminal, press r to rebuild and q to quit. See Watch mode details.

Check mode

--check tells you whether a run would change the generated files on disk, and writes nothing. Use it in CI to fail a pipeline when someone changed the spec or the config and did not commit the new output. --diff also prints what would change.

Terminal
kubb generate --check          # exit 1 when a file is out of date
kubb generate --check --diff   # also print what would change

Check generated code in CI has the workflows. See Check mode details and Exit codes.

Reporters

A reporter decides how you see a run. The config's reporters registers all four by default, and --reporter picks which ones run.

ReporterOutput
cliThe end-of-run summary in the terminal. Plain text in non-interactive terminals and AI agent sessions.
jsonA machine-readable report on stdout for CI. The run log goes to stderr. See Machine-readable output for the shape.
fileThe summary, plugin timings, and problems in .kubb/kubb[-<name>]-<timestamp>.log. <name> is the config's name, if set.
htmlA browsable report of the generated files and diagnostics in .kubb/kubb[-<name>]-<timestamp>/index.html.
Terminal
kubb generate --reporter file       # write a log file
kubb generate --reporter json       # print a JSON report for CI
kubb generate --reporter cli,html   # run two reporters

The cli summary ends like oxlint or pnpm: the details first, then a verdict line with the file counts, the kinds of change, and a Finished in line with the duration, the output path, and the plugins:

Summary
✓ Generated 18 files · 54 unchanged (12 via kubb-lock.json)
  14 added · 3 modified · 1 deleted · +312 −47
  Finished in 466ms · ./src/gen · 4 plugins

See Run summary.

Exit codes

CodeWithout --checkWith --check or --diff
0The run succeeds.The files on disk are up to date.
1The config fails to load, or any config fails to generate.A file would be added, modified, or deleted.
2The config fails to load, or any config fails to generate.

One failing config does not stop the others.

Less common cases

Watch mode details

With kubb generate --watch, Kubb watches only a local input file. A URL or git input generates once, so run Kubb again when the remote spec changes. Kubb does not watch the files the input references with $ref, such as schemas/Pet.yaml. After you edit one, press r or run Kubb again.

EventWhat happens
The first build failsKubb keeps watching, so you can fix the input.
A rebuild endsA line shows the duration and the schemas and operations the spec gained or lost. The summary below it lists the files the rebuild added, modified, or removed.
You press rKubb rebuilds. Keys work in an interactive terminal.
You press qKubb quits with exit code 0.
You press Ctrl+CKubb quits with exit code 130.

To keep clean off during incremental runs, pass a function to defineConfig and read watch. See Config function.

Adapter sources

When you write an adapter, its sources member lists the files and URLs the last parse() read, even unreadable ones. A watcher can rebuild when one of them changes, but kubb generate --watch does not read them. See Adapters.

Check mode details

Kubb generates in memory and compares each file with the one on disk. It writes nothing and runs no formatter or linter.

A formatter rewrites what Kubb generates, so the generated text and the file on disk often differ. kubb-lock.json records what each generated file became after output.format, output.lint, and output.postGenerate. A file is up to date when:

  • the file on disk holds the generated text, or
  • kubb-lock.json lists the file, and its two hashes equal the hash of the generated text and the hash of the file on disk.

Without output.format, output.lint, and output.postGenerate, the check needs no kubb-lock.json.

Out of date
  modified  src/gen/types/Pet.ts    +2 −1
  added     src/gen/types/Order.ts  +14 −0
  deleted   src/gen/types/Tag.ts    +0 −3

✗ 3 files out of date · 70 up to date (68 via kubb-lock.json)
  1 added · 1 modified · 1 deleted · +16 −4
  Run `kubb generate` to update them, or `kubb generate --diff` to see the changes.
  Finished in 412ms · ./src/gen · 4 plugins

Each row shows the status, the path from root, and the lines a run would add and remove. With --diff, the hunks of each file follow its row. Warnings, such as a missing kubb-lock.json, come before the verdict. When every file is up to date, the summary is two lines:

Up to date
✓ 71 files up to date · 68 via kubb-lock.json
  Finished in 407ms · ./src/gen · 4 plugins
StatusWhat a run would do
addedCreate the file. It is missing on disk.
modifiedRewrite the file. Its content on disk differs.
deletedRemove the file. Only listed with output.clean, which removes files Kubb no longer generates.

With --reporter json, each report holds check.changed, check.files, and check.lockfile. --diff then prints the diffs to stderr. See Machine-readable output.

--check differs from --dry-run:

--dry-run--check
Exit code when files would change01
Lists each out-of-date file with its pathNoYes
Prints a diff with --diffNoYes

--dry-run and --check run no formatter or linter. Each reads kubb-lock.json to recognize formatted output.

--check has these limits:

  • It cannot run with --watch. With --dry-run, it behaves like --check alone.
  • The config must use the default fs storage.
  • With output.format, output.lint, or output.postGenerate, the line counts and --diff compare the file on disk with the code before those steps ran. A changed file can show more changed lines than a run would write.
  • A new formatter version or formatter config does not make the check fail. Run kubb generate --force after you change one.

Up-to-date runs

kubb generate skips the build when nothing it reads has changed since the last run. Like pnpm, it prints "kubb-lock.json is up to date, generation is skipped" and exits with 0. Pass --force to generate anyway.

A run is up to date when all of these match kubb-lock.json:

  • The config, with the source of every function in it.
  • The spec, and every local file it references with $ref.
  • The version of Kubb and of every package in the dependencies and devDependencies of each package.json from root up.
  • Every generated file on disk.

Kubb always generates in these cases:

  • The input is a URL or a git input, or the spec references a URL. Kubb cannot compare those without fetching them.
  • The adapter does not list the files it read, or no package.json exists in root or above it.
  • The config sets output.postGenerate.
  • The config uses a storage other than fs.
  • You pass --watch, --check, --dry-run, or a reporter other than cli.
Kubb does not see a change in a file that your config imports, such as a helper module or a plugin you develop locally under the same version. It also does not see a change in your formatter's or linter's config file. Run kubb generate --force after you edit one.

A run that still generates, such as one with --reporter json, skips output.format and output.lint when nothing it reads changed and every file comes out unchanged. --force runs them.

kubb-lock.json

kubb generate writes kubb-lock.json to root on every run that writes files. It lists every generated file. Commit it with the generated files. To collapse it in pull requests, see Keep generated diffs reviewable.

--check and --dry-run read it, so they work on a fresh checkout without running a formatter.

kubb-lock.json
{
  "lockfileVersion": 1,
  "inputs": {
    "src/gen": {
      "hash": "0c3bf1dc…",
      "sources": ["openapi.yaml"]
    }
  },
  "files": {
    "src/gen/types/Pet.ts": {
      "source": "647596c2…",
      "output": "002220b7…"
    }
  }
}
FieldValue
lockfileVersionThe format version. Kubb ignores a file with another version.
inputsWhat each config last generated from, keyed by the config's name, or its output.path without one. Left out for an input Kubb cannot compare, such as a URL.
hashSHA-256 hash of the config, the package versions, and the content of the sources. Line endings count as \n.
sourcesThe spec and the local files it references, as paths from root.
filesOne entry per generated file, keyed by its path from root.
sourceSHA-256 hash of the code Kubb generated.
outputSHA-256 hash of the file on disk after output.format, output.lint, and output.postGenerate. Line endings count as \n.
  • Do not edit kubb-lock.json. After a merge conflict, run kubb generate and commit the result.
  • When --check finds formatted output and no kubb-lock.json, it exits with 1 and tells you to run kubb generate.

Generate without kubb-lock.json

Pass --no-lockfile to generate without the lockfile, as pnpm install --no-lockfile does:

kubb generate --no-lockfile
  • Kubb does not read kubb-lock.json and does not write it. An existing file stays as it is.
  • Every run generates, formats, and lints. No run is skipped as up to date.
  • A formatted file is written again on each run, because Kubb cannot tell that the formatter produced it.
  • --check and --dry-run count a file as unchanged only when the file on disk holds the generated text. --check --no-lockfile exits with 1 when a file differs from that text, which output.format, output.lint, or output.postGenerate usually causes.

Run summary

When a run ends, the cli reporter prints a summary. Warnings come first, then a verdict line, the kinds of change, what to run next, and a Finished in line.

Summary
✓ Generated 18 files · 54 unchanged (12 via kubb-lock.json)
  14 added · 3 modified · 1 deleted · +312 −47
  Finished in 466ms · ./src/gen · 4 plugins
EntryWhat it counts
unchangedFiles whose content did not change, also when output.clean rewrites the folder.
via kubb-lock.jsonUnchanged files that differ from the generated text but match through kubb-lock.json, such as formatted files. Like the "reused" count of a package manager.
added, modified, deletedThe output on disk compared with its content before the run. Only the kinds that happened are listed. Custom storage drivers leave this line out.
+312 −47The count of each line, so moving a line inside a file is not a removal plus an addition.

With --dry-run, the verdict starts with ◇ Dry run: and says how many files would change, followed by "Nothing was written."