Croner: JavaScript Cron Scheduler

10.0.1 · active · verified Sun Apr 19

Croner is a zero-dependency JavaScript library designed for scheduling and executing functions based on cron expressions across all JavaScript environments, including Node.js (v18.0+) and modern browsers. The current stable version is 10.0.1, with a consistent release cadence for bug fixes and minor enhancements, as indicated by recent development builds. A major differentiator is its full compliance with OCPS 1.4, offering advanced scheduling capabilities like an optional seventh field for year specification and robust handling of complex cron patterns. Version 10 introduced features such as the `match()` method for pattern validation, `dayOffset` for relative scheduling, and improved DST fall-back behavior. It stands out for its isomorphic design and comprehensive feature set without external dependencies, providing a reliable and lightweight solution for various task scheduling needs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create recurring and one-time cron jobs, retrieve next execution times, and stop a running job.

import { Cron } from 'croner';

// Schedule a job to run every minute
const job = new Cron('* * * * *', () => {
  console.log(`Cron job executed at ${new Date().toISOString()}`);
});

// Schedule a job to run once on a specific date in the future
const oneTimeJob = new Cron(new Date(Date.now() + 1000 * 60 * 5), () => {
  console.log(`One-time job executed after 5 minutes at ${new Date().toISOString()}`);
  oneTimeJob.stop(); // Stop the job after it runs once
}, { timezone: 'Europe/Stockholm', name: 'One-Time Task' });

// Enumerate upcoming runs
console.log('Next 5 runs for the minute job:', job.nextRuns(5).map(d => d.toISOString()));

// Stop the minute job after 10 minutes for demonstration
setTimeout(() => {
  job.stop();
  console.log('Cron job stopped after 10 minutes.');
}, 1000 * 60 * 10);

view raw JSON →