node-schedule

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 flexible library for scheduling tasks (cron-like) in the Node.js environment. It allows you to execute JavaScript/TypeScript functions on a schedule using cron syntax, intervals, or specific dates. The library is ideal for creating background tasks, periodic notifications, data cleanup, and other automated processes.

Why is node-schedule needed?

In web applications, code often needs to be executed not on user request, but by time. For example: sending daily reports, archiving logs, checking server statuses. node-schedule provides a simple and reliable way to implement this without complex queue systems. Unlike setInterval, it supports complex cron schedules and takes time zones into account.

Installation

Install the library via npm or yarn:

npm install node-schedule# oryarn add node-schedule

For TypeScript, additionally install the types:

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

Main Features

  • Cron Schedules: Support for standard cron syntax (minutes, hours, days, months).
  • Job Cancellation: Ability to cancel a scheduled execution.
  • One-time Jobs: Execution on a specific date.
  • Recurring Jobs: Repetition at a fixed interval.
  • Error Handling: Built-in support for error callbacks.

TypeScript Code Example

import * as schedule from 'node-schedule';

// Job: every minuteconst job = schedule.scheduleJob('*/1 * * * *', () => { console.log('Job executed at:', new Date().toISOString());});

// One-time job after 10 secondsconst date = new Date(Date.now() + 10000);schedule.scheduleJob(date, () => { console.log('One-time job');});

// Cancel job after 30 secondssetTimeout(() => { job.cancel(); console.log('Job cancelled');}, 30000);

// Job with a recurring ruleconst rule = new schedule.RecurrenceRule();rule.hour = 14;rule.minute = 30;schedule.scheduleJob(rule, () => { console.log('Job at 14:30 every day');});

When to Use

node-schedule is ideal for:

  • Background Utilities: cleaning temporary files, backups.
  • Notifications: sending emails or push notifications on a schedule.
  • Monitoring: periodic health checks of services.
  • Integrations: synchronization with external APIs via cron.

Do not use node-schedule for tasks requiring high precision (milliseconds) or distributed execution (queues like Bull or RabbitMQ are better suited for this).

The library is lightweight, requires no external dependencies, and works excellently with Express, NestJS, or plain Node.js.

Recommendations