denoland/deno · error · TypeError

Cannot create cron job, a single handler is required: two…

Error message

Cannot create cron job, a single handler is required: two handlers were specified

What it means

Deno.cron has two overloads: (name, schedule, handler) and (name, schedule, options, handler). If the third argument is a function AND a fourth argument is also supplied (not undefined), the implementation cannot tell which is the handler and throws the 'two handlers' TypeError.

Solutions

  1. Use exactly one handler: `Deno.cron('n', schedule, fn)` or `Deno.cron('n', schedule, { backoffSchedule, signal }, fn)`
  2. Delete the duplicate function argument

Example fix

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

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

Strategy: validation

Validate before calling

const fns = [handlerOrOptions1, handler2].filter((x) => typeof x === "function");
if (fns.length > 1) {
  throw new TypeError("pass at most one handler");
}

Prevention

When it happens

Trigger: `Deno.cron("n", "* * * * *", fn, fn2)`; or passing (name, schedule, handler, options) in the wrong order so both the third and fourth slots contain functions.

Common situations: Migrating from an API shape that took options fourth and handler third; copy-paste duplication of the callback; refactoring call sites between the two overloads.

Related errors


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

Appendix: source

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

    );
  }
  if (schedule === undefined) {
    throw new TypeError(
      "Cannot create cron job, a schedule is required: received 'undefined'",
    );
  }

  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;

View on GitHub (pinned to a961cdec3b)