TypeScript Task Scheduler: node-schedule Library

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

node-schedule Library: A Flexible Task Scheduler in TypeScript



In modern web development, there is often a need to perform certain actions at a specified time or on a schedule. For the Node.js and TypeScript ecosystem, there are several solutions, but node-schedule stands out for its flexibility and simplicity. This library allows you to create both one-time tasks for a specific date and recurring tasks using cron expressions or human-readable intervals.



Key Features of node-schedule



  • Precise execution time – run a task at a specific moment in time (date + time)
  • Cron expressions – classic syntax for periodic tasks (every minute, every hour, etc.)
  • Recurrence rules – flexible configuration of repetitions using the RecurrenceRule object
  • Cancellation and pausing – manage active tasks
  • Timezone support – work with local time


Installation and Setup



To get started, install the package via npm:



npm install node-schedulenpm install @types/node-schedule --save-dev


Import in a TypeScript file:



import * as schedule from 'node-schedule';


Example 1: Task for a Specific Date



Let's create a task that will run on December 31, 2024 at 23:59:59:



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

const job = schedule.scheduleJob(date, () => { console.log('🎉 Happy New Year 2025!');});

// Cancel the task (if needed)// job.cancel();


Example 2: Periodic Task with a Cron Expression



Run a task every 5 minutes:



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


Cron expression format: second minute hour day_of_month month day_of_week. node-schedule supports all 6 fields (unlike classic cron with 5 fields).



Example 3: Using RecurrenceRule



Let's create a task that runs every Tuesday and Thursday at 10:30 AM:



const rule = new schedule.RecurrenceRule();rule.dayOfWeek = [2, 4]; // Tuesday and Thursdayrule.hour = 10;rule.minute = 30;

const job = schedule.scheduleJob(rule, () => { console.log('Sending weekly report');});


RecurrenceRule supports the following fields:



  • year – a year or an array of years
  • month – month (0-11) or an array
  • date – day of the month (1-31)
  • dayOfWeek – day of the week (0-6, where 0 is Sunday)
  • hour – hour (0-23)
  • minute – minute (0-59)
  • second – second (0-59)


Example 4: Combined Rules



A task that runs on the last day of each month at 23:59:



const rule = new schedule.RecurrenceRule();rule.month = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // all monthsrule.date = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // last days of each monthrule.hour = 23;rule.minute = 59;rule.second = 59;

const job = schedule.scheduleJob(rule, () => { console.log('Running on the last day of the month');});


Task Management



The library provides methods for full control over tasks:



// Create a taskconst job = schedule.scheduleJob('*/10 * * * * *', () => {    console.log('Every 10 seconds');});

// Cancel a taskjob.cancel();

// Check if a task is activeconsole.log(job.pending()); // true or false

// Cancel all tasksschedule.gracefulShutdown

Recommendations