MCPcopy
hub / github.com/node-cron/node-cron

github.com/node-cron/node-cron @v4.5.0 sqlite

repository ↗ · DeepWiki ↗ · release v4.5.0 ↗
294 symbols 735 edges 69 files 25 documented · 9%
README

Node Cron

npm npm NPM Downloads Coverage Status

Job scheduling for Node.js with overlap prevention, distributed coordination, and background tasks. Schedule recurring tasks with cron expressions, prevent overlapping runs, coordinate across multiple instances, and run heavy jobs in isolated background processes. Zero dependencies, written in TypeScript.

Full documentation: nodecron.com

Getting Started

npm install node-cron
import cron from 'node-cron';

cron.schedule('* * * * *', () => {
  console.log('running a task every minute');
});

Overlap Prevention

Long-running tasks can overlap when the next tick fires before the previous run finishes. noOverlap skips a run instead of stacking them:

cron.schedule('* * * * *', async () => {
  await slowJob();
}, { noOverlap: true });

Distributed Coordination

Running multiple instances of your app? distributed: true ensures only one instance executes each scheduled fire. Out of the box it uses an env-var flag; for high availability, plug in a Redis coordinator:

cron.schedule('0 3 * * *', runNightlyBackup, {
  name: 'nightly-backup',
  distributed: true,
});

Background Tasks

Pass a file path instead of a function to run a job in an isolated forked process, so heavy work never blocks your event loop:

cron.schedule('0 3 * * *', './tasks/backup.js');

Runtime Control

Every task exposes a single consistent interface for control and inspection:

const task = cron.schedule('0 3 * * *', doWork, {
  name: 'nightly-backup',
  timezone: 'America/Sao_Paulo',
});

task.stop();          // pause
task.start();         // resume
task.destroy();       // remove permanently
task.getStatus();     // 'stopped' | 'idle' | 'running' | 'destroyed'
task.getNextRun();    // next scheduled Date, or null
task.lastRun();       // { date, result } or { date, error }, or null

Events

Tasks emit lifecycle events for observability:

task.on('execution:finished', (ctx) => console.log('result:', ctx.execution?.result));
task.on('execution:failed', (ctx) => console.error('failed:', ctx.execution?.error));
task.on('execution:overlap', () => console.warn('skipped: previous run still active'));
task.on('execution:skipped', (ctx) => console.log('not elected:', ctx.reason));

All events: task:started, task:stopped, task:destroyed, execution:started, execution:finished, execution:failed, execution:missed, execution:overlap, execution:maxReached, execution:skipped. See Events & Observability.

Cron Syntax

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # * * * * * *
field value
second 0-59 (optional)
minute 0-59
hour 0-23
day of month 1-31 (or L for the last day)
month 1-12 (or names)
day of week 0-7 (or names, 0 or 7 are Sunday; 2#3, 5L)

Supports ranges (1-5), steps (*/2), lists (1,15), named months/weekdays, L (last day of month), # (nth weekday), and <weekday>L (last weekday of month). See the Cron Syntax guide.

When to Use node-cron

  • Recurring jobs on a schedule (cron expressions with second-level precision)
  • Overlap prevention for long-running tasks
  • Coordinating scheduled tasks across multiple instances or replicas
  • Running heavy jobs in isolated background processes
  • Runtime control: start, stop, inspect, and observe tasks programmatically

When to Consider Something Else

  • Durable job queues with retries and priorities: use BullMQ, Agenda, or Sidequest
  • Persistent workflow orchestration: use Temporal or Inngest
  • Exactly-once guarantees across crashes: node-cron coordinates but does not persist state to a database; a queue or workflow engine is a better fit

Options

cron.schedule('0 3 * * *', task, {
  name: 'nightly-backup',
  timezone: 'America/Sao_Paulo',
  noOverlap: true,
  distributed: true,
  maxExecutions: 10,
  maxRandomDelay: 30000,
});

See Scheduling Options for the full list.

Migrating from v3

v4 is a TypeScript rewrite with a smarter scheduler and a streamlined API. See the Migration Guide.

Issues

Feel free to submit issues and enhancement requests here.

Contributing

In general, we follow the "fork-and-pull" Git workflow.

  • Fork the repo on GitHub;
  • Commit changes to a branch in your fork;
  • Pull request "upstream" with your changes;

NOTE: Be sure to merge the latest from "upstream" before making a pull request!

Please do not contribute code you did not write yourself, unless you are certain you have the legal ability to do so. Also ensure all contributed code can be distributed under the ISC License.

License

node-cron is under ISC License.

Extension points exported contracts — how you extend this code

ScheduledTask (Interface)
(no doc) [4 implementers]
src/tasks/scheduled-task.ts
RunCoordinator (Interface)
(no doc) [3 implementers]
src/coordinator/run-coordinator.ts
NodeCron (Interface)
(no doc)
src/node-cron.ts
Logger (Interface)
(no doc)
src/logger.ts
CronFieldError (Interface)
(no doc)
src/pattern/validation/pattern-validation.ts
NthWeekday (Interface)
(no doc)
src/time/day-of-week.ts
ParsedFields (Interface)
(no doc)
src/pattern/validation/pattern-validation.ts
DetailedValidation (Interface)
(no doc)
src/pattern/validation/pattern-validation.ts

Core symbols most depended-on inside this repo

match
called by 153
src/tasks/scheduled-task.ts
start
called by 65
src/tasks/scheduled-task.ts
validate
called by 60
src/pattern/validation/pattern-validation.ts
destroy
called by 44
src/tasks/scheduled-task.ts
stop
called by 36
src/tasks/scheduled-task.ts
getNextMatch
called by 34
src/time/time-matcher.ts
on
called by 25
src/tasks/scheduled-task.ts
toISO
called by 23
src/time/localized-time.ts

Shape

Function 138
Method 122
Class 26
Interface 8

Languages

TypeScript100%

Modules by API surface

src/tasks/background-scheduled-task/background-scheduled-task.ts34 symbols
src/tasks/inline-scheduled-task.ts26 symbols
src/scheduler/runner.ts20 symbols
src/tasks/scheduled-task.ts17 symbols
src/time/localized-time.ts16 symbols
src/pattern/validation/pattern-validation.ts14 symbols
src/promise/tracked-promise.ts13 symbols
src/logger.ts13 symbols
src/time/matcher-walker.ts10 symbols
src/time/day-of-week.ts9 symbols
src/task-registry.ts8 symbols
src/coordinator/run-coordinator.test.ts8 symbols

Dependencies from manifests, versioned

@eslint/js9.26.0 · 1×
@rollup/plugin-commonjs29.0.3 · 1×
@rollup/plugin-node-resolve16.0.3 · 1×
@rollup/plugin-replace6.0.3 · 1×
@rollup/plugin-typescript12.3.0 · 1×
@types/chai5.2.1 · 1×
@types/expect1.20.4 · 1×
@types/node22.15.3 · 1×
@types/sinon17.0.4 · 1×
@typescript-eslint/eslint-plugin8.32.0 · 1×
@typescript-eslint/parser8.32.0 · 1×
@vitest/coverage-v84.1.9 · 1×

For agents

$ claude mcp add node-cron \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact