Skip to content

binding generator

The binding generator wires a binding into an existing Worker: it edits the Worker’s wrangler.jsonc, stubs any required code (Durable Object / Workflow classes, a queue consumer) and migrations, emits a matching test, and refreshes the generated Env types via wrangler types.

It targets an existing Worker — scaffold one first with the application generator.

bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=kv --binding=MY_KV --id=<namespace-id>

binding is the Env key your Worker reads (env.MY_KV), in SCREAMING_SNAKE_CASE.

TypeWhat it addsRequires
kvA kv_namespaces[] entry.--id (or --create)
r2An r2_buckets[] entry.--bucketName (or --create)
d1A d1_databases[] entry.--databaseName (or --create)
doA durable_objects.bindings[] entry + a [[migrations]] tag, a class stub, and a re-export.--name (the class name)
queueA queues.producers[] + queues.consumers[] entry, and injects a queue() handler.--name (the queue name)
workflowA workflows[] entry + a WorkflowEntrypoint class stub and a re-export.--name (the class name)
serviceA services[] entry referencing another Worker.--name (the target service)

Config-only bindings. Pass the existing resource id/name, or --create to provision it:

# KV with an existing namespace
bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=kv --binding=CACHE --id=abc123

# R2, provisioning the bucket via the Wrangler CLI
bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=r2 --binding=ASSETS --create

# D1 with an existing database
bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=d1 --binding=DB --databaseName=app-db --id=<database-id>
bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=do --binding=COUNTER --name=Counter

Writes src/counter.ts (a DurableObject<Env> subclass), re-exports it from src/index.ts (Wrangler requires the class to be exported from the Worker’s entrypoint), and appends a SQLite migration with an incremented tag (v1, v2, …).

bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=queue --binding=JOBS --name=jobs-queue

Adds both a producer and a consumer for the queue, and injects an async queue(batch, env, ctx) handler into the Worker’s default export. A Worker has a single queue() handler that receives batches from all of its queue consumers — if one already exists, it’s left untouched.

bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=workflow --binding=MY_WORKFLOW --name=MyWorkflow
bunx nx g @naxodev/nx-cloudflare:binding --project=my-worker --type=service --binding=AUTH --name=auth-worker
OptionTypeDefaultDescription
projectstring(required)The target Worker project. Defaults to the first positional argument’s project.
typekv | r2 | d1 | do | queue | workflow | service(required)The kind of binding to add.
bindingstring(required)The Env key (SCREAMING_SNAKE_CASE) the Worker uses to access the binding, e.g. MY_KV.
namestringRequired for do/workflow (the class name), queue (the queue name), and service (target).
idstringExisting KV namespace id or D1 database id. Ignored when --create is set.
databaseNamestringD1 database name. Required for --type=d1 without --create.
bucketNamestringR2 bucket name. Required for --type=r2 without --create.
createbooleanfalseProvision the remote resource via the Wrangler CLI (KV/R2/D1/Queue only) and fill in the id.
skipTestsbooleanfalseSkip generating a Vitest spec for the binding stub.
skipFormatbooleanfalseSkip formatting files.
skipTypegenbooleanfalseSkip auto-running wrangler types — the Env interface will not be refreshed.
  • JSONC only. The generator edits wrangler.jsonc/wrangler.json while preserving comments and formatting. wrangler.toml is rejected with a pointer to convert first.
  • wrangler types runs automatically after the binding is written so Env picks up the new binding. Pass --skipTypegen to opt out (run nx typegen <project> yourself later).
  • --create is only valid for KV/R2/D1/Queue — Durable Objects and Workflows are code in your Worker, and a Service binding references another Worker, so none of them are provisioned resources. On a provisioning failure the generator fails loudly and leaves a placeholder in the config.
  • Duplicate bindings are rejected, never overwritten. The generator also rejects a Durable Object whose class name is already defined by an existing binding or migration.

The Worker’s wrangler.jsonc now contains the new entry — for --type=kv, a kv_namespaces array holding your binding. Unless you passed --skipTypegen, the regenerated Env (via wrangler types) includes it too.