How to Schedule Cron Jobs in Node.js
This article explains how to automate repetitive background tasks in
a Node.js application. You will learn how to schedule tasks using
popular lightweight libraries like node-cron and
node-schedule, understand standard cron syntax, and explore
best practices for managing scheduled jobs in production
environments.
Choosing the Right Tool
To run background tasks in Node.js, you generally rely on npm packages rather than the operating system’s built-in tab manager. The two most common libraries are:
- node-cron: Best for traditional, interval-based cron expressions (e.g., “run every Monday at 5:00 AM”).
- node-schedule: Best for time-based scheduling and precise date-based scheduling (e.g., “run on December 25th, 2026”).
Method 1: Using
node-cron
node-cron is a tiny, pure JavaScript scheduler that uses
the standard crontab syntax.
1. Installation
Install the package using npm:
npm install node-cron2. Implementation
Create a basic scheduler using the code below:
const cron = require('node-cron');
// Schedule a task to run every minute
cron.schedule('* * * * *', () => {
console.log('Running a task every minute: ' + new Date().toISOString());
});Understanding Cron Syntax
The five asterisks (* * * * *) represent different time
units:
┌────────────── second (optional, node-cron supports 6 fields)
│ ┌──────────── minute (0 - 59)
│ │ ┌────────── hour (0 - 23)
│ │ │ ┌──────── day of month (1 - 31)
│ │ │ │ ┌────── month (1 - 12)
│ │ │ │ │ ┌──── day of week (0 - 7) (0 or 7 is Sunday)
│ │ │ │ │ │
* * * * * *
0 0 * * *: Runs every day at midnight.*/15 * * * *: Runs every 15 minutes.0 9 * * 1-5: Runs at 9:00 AM, Monday through Friday.
Method 2: Using
node-schedule
If you need to schedule tasks based on specific dates or complex
rules rather than strict cron intervals, node-schedule is
the preferred choice.
1. Installation
npm install node-schedule2. Implementation
You can schedule tasks using object literal syntax or standard cron strings.
const schedule = require('node-schedule');
// Schedule a task to run at a specific date and time
const date = new Date(2025, 11, 25, 10, 0, 0); // Dec 25, 2025, at 10:00 AM
const job = schedule.scheduleJob(date, function() {
console.log('Merry Christmas! Task executed.');
});
// Schedule a task using recurrence rules (Every hour, at the 30-minute mark)
const rule = new schedule.RecurrenceRule();
rule.minute = 30;
const recurringJob = schedule.scheduleJob(rule, function() {
console.log('This job runs at half-past every hour.');
});Best Practices for Production
While memory-based schedulers like node-cron and
node-schedule work perfectly for single-instance servers,
they have limitations in production:
- Avoid Multi-Instance Duplication: If you run your Node.js app in a clustered environment (like PM2 or multiple Docker containers behind a load balancer), memory-based cron jobs will trigger on every instance, causing duplicate executions.
- Handle Server Crashes: Memory-based jobs do not persist. If your server restarts, any pending one-off jobs scheduled in memory will be lost.
Scaling with Queue Systems
For multi-instance environments or resource-heavy background tasks, use a database-backed queue system: * BullMQ / Bull: A Redis-based queue for Node.js that handles delayed jobs, cron-like repeats, and guarantees that a job is executed only once across a cluster. * Agenda: A MongoDB-backed job scheduler that persists jobs across server restarts.