node-schedule — task scheduler for TypeScript

онлайн тренажер по питону
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

What is node-schedule?

node-schedule is a popular library for scheduling delayed and recurring tasks in a Node.js environment. It is written in JavaScript but works perfectly with TypeScript thanks to type definitions (DefinitelyTyped). The library allows you to run functions at a specific time, on a schedule (cron-like syntax), or at set intervals.

Why do you need node-schedule?

In modern web applications, background operations are often required: cache clearing, sending email newsletters, database backups, updating data from external APIs, or generating reports. Node-schedule solves this problem without using external services (e.g., cron in Linux) — everything works directly inside your TypeScript application.

Installation

Install the library and types via npm:

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

After that, you can import it into your TypeScript project:

import * as schedule from 'node-schedule';

Main Features

  • Scheduling by cron string — specify minutes, hours, days, months, and weekdays.
  • Scheduling for a specific date — pass a Date object or a string.
  • Recurrence with an interval — for example, every 10 minutes.
  • Cancelling a task — you can cancel a scheduled execution.
  • Checking for active tasks — the scheduledJobs method returns all active tasks.

TypeScript Code Example

import * as schedule from 'node-schedule';

// A task that will run every minute (at the 30th second)const job = schedule.scheduleJob('30 * * * * *', () => { console.log('Task executed at:', new Date().toLocaleString());});

// A task for a specific dateconst date = new Date(Date.now() + 5000); // in 5 secondsschedule.scheduleJob(date, () => { console.log('One-time task in 5 seconds');});

// Recurrence every 10 secondsconst intervalJob = schedule.scheduleJob('*/10 * * * * *', () => { console.log('Every 10 seconds');});

// Cancel the task after 20 secondssetTimeout(() => { job.cancel(); console.log('First task cancelled');}, 20000);

// Check active tasksconsole.log('Active tasks:', Object.keys(schedule.scheduledJobs));

When to use node-schedule

  • When you need to run tasks on a schedule inside a Node.js application without an external cron.
  • For simple background tasks: log cleaning, sending emails, cache updating.
  • When you need the flexibility of cron syntax (e.g., every first day of the month at 3:00 AM).
  • If you do not want to depend on the operating system (cross-platform compatibility).

Do not use node-schedule for high-load distributed systems — in such cases, task queues (Bull, RabbitMQ) or cloud schedulers (AWS CloudWatch, GCP Cloud Scheduler) are better suited.

The library is simple, lightweight, and ideal for small and medium-sized TypeScript projects.

Recommendations