node-cron
Cron for NodeJS.
node-cron is a Node.js library that schedules code to run at specific times or on repeating intervals using cron syntax, handling everything from daily jobs to sub-second tasks.
node-cron is a TypeScript library for Node.js that lets you schedule code to run automatically at specific times or on repeating schedules. If you want your application to send a daily email at 8am, clean up old files every Sunday, or run a data backup every hour, this library handles that scheduling logic for you.
Schedules are written using cron syntax, a compact notation originally from Unix systems. A pattern like 0 8 * * * means "at 8:00am every day." The library extends the standard five-field format with an optional sixth field for seconds, so you can schedule tasks down to the second if needed. You can also pass a JavaScript Date object or a Luxon DateTime object instead of a cron string if you prefer to work with dates directly.
The main building block is the CronJob class. You create a job by providing a schedule, a function to run, and optional settings like the time zone and whether to start the job immediately. Jobs can be started and stopped programmatically. There are also standalone utility functions: one tells you when a given cron expression will next fire, one returns the number of milliseconds until the next trigger, and one validates whether a cron expression is written correctly.
Version 3 introduced TypeScript support and aligned month and day-of-week numbering with standard Unix cron conventions. Version 4 dropped support for Node.js 16 and made the job's running-state property read-only. The README includes a migration guide covering the specific changes between major versions.
The library works with Node.js 18 and above and is available on npm. It is a common choice for background task scheduling in Node.js applications.
Where it fits
- Schedule a Node.js function to send daily emails at 8am using a simple cron expression and time zone setting.
- Run database cleanup or backup tasks on a recurring weekly schedule inside a Node.js application.
- Validate cron expressions and preview the next scheduled trigger time before deploying a job to production.
- Control job start and stop programmatically, enabling dynamic scheduling based on application state or user settings.