denoland/deno · error · TypeError

Cannot create cron job, a schedule is required: received 'un

Error message

Cannot create cron job, a schedule is required: received 'undefined'

What it means

The second argument to Deno.cron is mandatory: a cron expression string ('*/5 * * * *') or a Deno.CronSchedule object. An undefined schedule throws before the job is created.

Source

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

      " " + formatToCronSchedule(dayOfWeek);
  }
}

function cron(
  name: string,
  schedule: string | Deno.CronSchedule,
  handlerOrOptions1:
    | (() => Promise<void> | void)
    | ({ backoffSchedule?: number[]; signal?: AbortSignal }),
  handler2?: () => Promise<void> | void,
) {
  if (name === undefined) {
    throw new TypeError(
      "Cannot create cron job, a unique name is required: received 'undefined'",
    );
  }
  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",
      );
    }

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Pass a cron string or schedule object as the second argument.
  2. Validate that the schedule config exists before registering the job.

Example fix

// before
Deno.cron('n', cfg.schedule, fn); // cfg.schedule is undefined
// after
if (!cfg.schedule) throw new Error('cron job requires cfg.schedule');
Deno.cron('n', cfg.schedule, fn);
Defensive patterns

Strategy: validation

Validate before calling

if (schedule === undefined) {
  throw new Error('cron job schedule is required');
}
Deno.cron(name, schedule, handler);

Type guard

function isCronScheduleValue(v: unknown): v is string | Deno.CronSchedule {
  return typeof v === 'string' || (typeof v === 'object' && v !== null);
}

Prevention

When it happens

Trigger: Deno.cron('n', undefined, fn); omitting the schedule argument entirely.

Common situations: Optional schedule settings in config; refactors that changed the parameter list.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/11f2424599159377. Report an issue: GitHub.