Task Scheduler in Node.js: Overview of node-cron, node-schedule and Bull Libraries
In modern Node.js web development, there is often a need to execute code on a schedule or with a delay. Whether it's sending email newsletters, clearing cache, generating reports, or synchronizing data — a reliable task scheduler is essential. In this article, we will cover three popular tools: node-cron, node-schedule, and Bull. You will learn their features, usage examples, and understand which one to choose for your task.
1. node-cron — Classic Cron for Node.js
node-cron is a minimalist library that implements the Unix cron scheduler syntax. It is ideal for simple recurring tasks.
Installation
npm install node-cronExample: a task running every minute
const cron = require('node-cron');
cron.schedule('* * * * *', () => { console.log('Task runs every minute');});Cron expression format
The expression consists of 5 fields:
second (0-59, optional)minute (0-59)hour (0-23)day of month (1-31)month (1-12)day of week (0-7, 0 and 7 = Sunday)
Advanced example: daily at 8:30 AM
cron.schedule('30 8 * * *', () => { // Send morning report sendMorningReport();});Features
- Timezone support via the
timezoneoption - Ability to start and stop tasks
- Lightweight (~20 KB)
- Not suitable for complex logic with queues
2. node-schedule — Flexible scheduler with recurring rules
node-schedule provides a richer API: you can define schedules using objects, not just cron strings. This is convenient for complex scenarios (e.g., "every second Tuesday of the month at 15:00").
Installation
npm install node-scheduleExample: task using a cron expression
const schedule = require('node-schedule');
const job = schedule.scheduleJob('*/5 * * * *', () => { console.log('Runs every 5 minutes');});Example: recurring rule using an object
const rule = new schedule.RecurrenceRule();rule.dayOfWeek = [1, 3, 5]; // Mon, Wed, Frirule.hour = 10;rule.minute = 30;
schedule.scheduleJob(rule, () => { console.log('Task on weekdays at 10:30');});Example: delayed execution
const date = new Date(Date.now() + 5000); // in 5 secondsschedule.scheduleJob(date, () => { console.log('One-time task in 5 seconds');});Features
- Flexible rule definition (days of week, months, ranges)
- Support for one-time tasks by date
- Ability to cancel a task:
job.cancel() - Does not support clustering and queues
3. Bull — Powerful queue with scheduler and retries
Bull is not just a scheduler, but a full-fledged queue system based on Redis. It is ideal for production environments: supports delayed tasks, retries on errors, priorities, and parallel processing.
Installation
npm install bull# Requires Redis to be installedExample: simple task with a delay
const Queue = require('bull');const myQueue = new Queue('myQueue', 'redis://127.0.0.1:6379');
// Add a task with a 1-hour delaymyQueue.add({ email: 'user@example.com' }, { delay: 3600000 });
// ProcessormyQueue.process(async (job) => { console.log(`Sending email for ${job.data.email}`); await sendEmail(job Also in library
Geolocation and Maps in C# (.NET): Overview of Libraries and Tools
Cryptography in Node.js: Encryption and Hashing with crypto
Caching in Java: Redis, Ehcache, Caffeine, Hazelcast — Overview and Examples
SQLDelight for Kotlin: Complete Guide with Examples