Node.js Task Scheduler (node-cron, node-schedule, Bull)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Task Scheduler in Node.js: Overview and Usage Examples



In modern backend development, there is often a need to execute code on a schedule: sending email newsletters, clearing cache, creating database backups, or updating aggregated data. In the Node.js ecosystem, there are several mature libraries for these purposes. In this article, we will cover three main approaches to task scheduling: classic cron, flexible scheduling, and task queues.



1. node-cron — Classic Cron for JavaScript



node-cron is a minimalist library that allows you to run functions using standard cron expressions. It is ideal for simple periodic tasks (every minute, every hour, every day at 3:00 AM).



Installation:



npm install node-cron


Example of a task that runs every minute:



const cron = require('node-cron');

cron.schedule('* * * * *', () => { console.log('Task runs every minute');});


Cron expression structure: second minute hour day_of_month month day_of_week (in node-cron, the second is optional).



Expression examples:

  • '0 0 * * *' — every day at midnight
  • '*/5 * * * *' — every 5 minutes
  • '0 9-18 * * 1-5' — every hour from 9 AM to 6 PM on weekdays
  • '30 3 * * 0' — every Sunday at 3:30 AM


The library supports expression validation and starting/stopping tasks:



const task = cron.schedule('*/10 * * * *', () => {  // task every 10 minutes});

// Stop the tasktask.stop();

// Resumetask.start();

// Check if the task is activeconsole.log(task.isStarted()); // true/false


When to use: for simple periodic actions where state persistence or error handling is not required.



2. node-schedule — Flexible Scheduling with Recurrence Rule Support



node-schedule is a more powerful alternative that allows you to define schedules not only via cron but also through JavaScript objects with explicit dates, days of the week, and times.



Installation:



npm install node-schedule


Example with a cron expression:



const schedule = require('node-schedule');

const job = schedule.scheduleJob('*/5 * * * *', () => { console.log('Runs every 5 minutes');});


The main advantage is the ability to define a schedule using an object:



const job = schedule.scheduleJob({  hour: 14,  minute: 30,  dayOfWeek: [1, 3, 5] // Monday, Wednesday, Friday}, () => {  console.log('14:30 on weekdays');});


You can also schedule a one-time task for a specific date:



const date = new Date(2025, 11, 31, 23, 59, 0);

schedule.scheduleJob(date, () => { console.log('New Year has arrived!');});


The library supports task cancellation and checking the next execution time:



// Cancel the taskjob.cancel();

// Get the next execution dateconsole.log(job.nextInvocation()); // Date object


When to use: when precise time control, rule combination, or one-time tasks are needed.



3. Bull — Task Queues with Scheduling and Retries



Bull is a full-featured queue system based on Redis, which includes a powerful scheduler. Bull allows you not only to delay tasks but also to manage retries, priorities, and parallel execution.



Installation:



npm install bull


Basic example of a queue with a delayed task:



const Queue = require('bull');

const emailQueue = new Queue('email', { redis: { host: 'localhost', port: 63

Recommendations