Task Scheduler in TypeScript: A Detailed Guide to the 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

TypeScript Task Scheduler: Automation with node-schedule



In modern web development, there is often a need to execute code on a schedule: sending notifications, clearing cache, generating reports, or synchronizing data. For TypeScript projects, one of the most powerful and flexible libraries is node-schedule. This is a task scheduler that allows you to set both one-time and recurring executions using cron expressions, dates, or custom rules.



Installation and Setup



To install the library, run the command:



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


After installation, you can import the module into your TypeScript file:



import * as schedule from 'node-schedule';


Key Features of node-schedule



  • Cron expression scheduling — support for classic 6-field masks (seconds, minutes, hours, day of month, month, day of week).
  • One-time tasks — execute code at a specific point in time.
  • Recurring rules — create custom intervals using the RecurrenceRule object.
  • Task cancellation — ability to stop the execution of a scheduled job.
  • Timezone support — precise execution in a specified time zone.


Example 1: Simple Cron Scheduler



Let's create a task that will run every minute:



import * as schedule from 'node-schedule';

const job = schedule.scheduleJob('*/1 * * * *', () => { console.log('Task executed:', new Date().toLocaleString());});

// Cancel the task after 5 minutessetTimeout(() => { job.cancel(); console.log('Task cancelled');}, 5 * 60 * 1000);


In this example, the cron expression '*/1 * * * *' means "every minute". Note the field order: seconds, minutes, hours, day of month, month, day of week.



Example 2: Task with a Specific Date



You can schedule execution for a specific time in the future:



import * as schedule from 'node-schedule';

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

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

// Check if the task is scheduledconsole.log('Task is active:', job !== null);


Example 3: Recurrence Rule (RecurrenceRule)



If cron expressions seem complex, use the RecurrenceRule object to create clear rules:



import * as schedule from 'node-schedule';

const rule = new schedule.RecurrenceRule();rule.hour = 9;rule.minute = 30;rule.dayOfWeek = [1, 3, 5]; // Monday, Wednesday, Friday

const job = schedule.scheduleJob(rule, () => { console.log('Morning meeting at 9:30 on weekdays (Mon, Wed, Fri)');});

// To cancel after a month:setTimeout(() => job.cancel(), 30 * 24 * 60 * 60 * 1000);


Example 4: Task with Timezone



If your application works in different time zones, specify the timezone:



import * as schedule from 'node-schedule';

const job = schedule.scheduleJob( { hour: 8, minute: 0, tz: 'Europe/Moscow' }, () => { console.log('Moscow morning: 8:00'); });


Error Handling and Task Cancellation



Always provide error handling inside the callback to avoid application crashes:



import * as schedule from 'node-schedule';

const job = schedule.scheduleJob('*/5 * * * *', async () => { try { // Your asynchronous code const data = await fetchDataFromAPI(); console.log('Data received:', data); } catch (error) { console.error('Error in scheduler:',

Recommendations