Task Scheduler in Node.js: node-schedule and node-cron
In modern web development, there is often a need to perform certain actions at a specified time or at regular intervals. For example, sending reports via email, cleaning a database, parsing external resources, or updating a cache. In the Node.js environment, task schedulers are used for these purposes. The two most popular tools are node-schedule and node-cron. In this article, we will analyze both in detail, provide code examples, and help you choose the right one for your project.
Why do you need a task scheduler in Node.js?
Node.js is a single-threaded environment with an event-driven architecture. The built-in setTimeout and setInterval functions allow you to execute code with a delay or periodically, but they are not suitable for complex scenarios:
- No flexibility in configuration (e.g., "every second Monday of the month at 10:30").
- They do not preserve state after an application restart.
- Difficulties with management (cancellation, schedule changes).
Schedulers solve these problems by providing a simple API for creating, running, and canceling scheduled tasks.
The node-schedule Library
node-schedule is a flexible scheduler based on a cron-like syntax but with extended capabilities. It allows you to set recurring tasks with minute precision, as well as one-time tasks for a specific date.
Installation
npm install node-scheduleMain Features
- Support for cron expressions (6 fields: seconds, minutes, hours, day of month, month, day of week).
- Ability to set one-time execution using a Date object.
- Recurrence rules using the
RecurrenceRuleobject. cancel()andcancelNext()methods for task management.
Example 1: Running a task every 5 minutes
const schedule = require('node-schedule');
const job = schedule.scheduleJob('*/5 * * * *', function() { console.log('Task runs every 5 minutes:', new Date().toLocaleString());});
// Cancel the task after 20 minutessetTimeout(() => { job.cancel(); console.log('Task cancelled');}, 20 * 60 * 1000);Example 2: Task for a specific date
const schedule = require('node-schedule');
const date = new Date(2025, 5, 15, 10, 30, 0); // June 15, 2025, 10:30:00
const job = schedule.scheduleJob(date, function() { console.log('One-time task executed at', new Date().toLocaleString());});
console.log('Task scheduled for', date.toLocaleString());Example 3: Complex recurrence rule
const schedule = require('node-schedule');
const rule = new schedule.RecurrenceRule();rule.dayOfWeek = [1, 3, 5]; // Monday, Wednesday, Fridayrule.hour = 9;rule.minute = 0;
const job = schedule.scheduleJob(rule, function() { console.log('Task runs at 9:00 on weekdays (Mon, Wed, Fri)');});The node-cron Library
node-cron is a lightweight alternative based on the classic cron from Unix systems. It is simpler to use but less flexible than node-schedule. It supports 5 fields (minutes, hours, day of month, month, day of week).
Installation
npm install node-cronMain Features
- Standard cron syntax (5 fields).
start(),stop(),destroy()methods.- Timezone support via the
timezoneoption. - Ability to run a task immediately using
now().
Example 1: Running a task every day at midnight
const c Also in library
Working with Markdown in Go: libraries blackfriday, goldmark and chroma
OAuth Client for C# (.NET): Implementing Authorization via Third-Party Services