denoland/deno · error · TypeError

Cannot create cron job: a handler is required

Error message

Cannot create cron job: a handler is required

What it means

After resolving Deno.cron's overloads, neither the third nor fourth argument position held a function - so there is no handler to run. This fires when only an options object is supplied (and no fourth-argument handler), or when arguments are misordered so the function never lands in a recognized slot.

Solutions

  1. Pass the handler: `Deno.cron('n', schedule, handler)`
  2. With options, the handler goes fourth: `Deno.cron('n', schedule, opts, handler)`
  3. Ensure the handler is a plain function reference, not wrapped in an object

Example fix

// before
Deno.cron("n", "* * * * *", { backoffSchedule: [1, 3, 5] });

// after
Deno.cron("n", "* * * * *", { backoffSchedule: [1, 3, 5] }, runJob);
Defensive patterns

Strategy: type-guard

Validate before calling

const hasHandler = [handlerOrOptions1, handler2].some((x) => typeof x === "function");
if (!hasHandler) {
  throw new TypeError("cron handler is required");
}

Type guard

function isCronHandler(x: unknown): x is () => void | Promise<void> {
  return typeof x === "function";
}

Prevention

When it happens

Trigger: `Deno.cron("n", "* * * * *")`; `Deno.cron("n", "* * * * *", { backoffSchedule: [1, 3, 5] })` (options without a fourth-argument handler); embedding the handler inside the options object.

Common situations: Forgetting the callback when adding cron jobs; assuming the handler can live inside the options bag; renaming a handler variable so `typeof x === 'function'` fails.

Related errors


AI-assisted analysis of denoland/deno@a961cdec3b (2026-08-20). Data as JSON: /api/errors/674506071562b04c. Report an issue: GitHub.

Appendix: source

Thrown at ext/cron/01_cron.ts:145

  schedule = parseScheduleToString(schedule);

  let handler: () => Promise<void> | void;
  let options:
    | { backoffSchedule?: number[]; signal?: AbortSignal }
    | undefined = undefined;

  if (typeof handlerOrOptions1 === "function") {
    handler = handlerOrOptions1;
    if (handler2 !== undefined) {
      throw new TypeError(
        "Cannot create cron job, a single handler is required: two handlers were specified",
      );
    }
  } else if (typeof handler2 === "function") {
    handler = handler2;
    options = handlerOrOptions1;
  } else {
    throw new TypeError("Cannot create cron job: a handler is required");
  }

  const rid = op_cron_create(
    name,
    schedule,
    options?.backoffSchedule,
  );

  if (options?.signal) {
    const signal = options?.signal;
    signal.addEventListener(
      "abort",
      () => {
        core.close(rid);
      },
      { once: true },
    );
  }

View on GitHub (pinned to a961cdec3b)