+0
日增长
8,954
stars
651
forks
99
watchers
474
prs
611
issues
55
releases
README
cron is a robust tool for running jobs (functions or commands) on schedules defined using the cron syntax.
Perfect for tasks like data backups, notifications, and many more!
Cron for Node.js
🌟 Features
- execute a function whenever your scheduled job triggers
- execute a job external to the javascript process (like a system command) using child_process
- use a Date or Luxon DateTime object instead of cron syntax as the trigger for your callback
- use an additional slot for seconds (leaving it off will default to 0 and match the Unix behavior)
🚀 Installation
bash
npm install cron
Table of Contents
1. Features
2. Installation
3. Migrating
4. Basic Usage
5. Cron Patterns
- Cron Syntax Overview
- Supported Ranges
6. API
- Standalone Functions
- CronJob Class
- CronTime Class
7. Gotchas
8. Community
- Join the Community
9. Contributing
- General Contribution
- Submitting Bugs/Issues
10. Acknowledgements
11. License
⬆ Migrating
v4 dropped Node v16 and renamed the job.running property:
Migrating from v3 to v4
Dropped Node version
Node v16 is no longer supported. Upgrade your Node installation to Node v18 or above
Property renamed and now read-only
You can no longer set the running property (now isActive). It is read-only. To start or stop a cron job, use job.start() and job.stop().
v3 introduced TypeScript and tighter Unix cron pattern alignment:
Migrating from v2 to v3
Month & day-of-week indexing changes
- Month Indexing: Changed from 0-11 to 1-12. So you need to increment all numeric months by 1.
- Day-of-Week Indexing: Support added for 7 as Sunday.
Adjustments in CronJob
- The constructor no longer accepts an object as its first and only params. Use CronJob.from(argsObject) instead.
- Callbacks are now called in the order they were registered.
- nextDates(count?: number) now always returns an array (empty if no argument is provided). Use nextDate() instead for a single date.
Removed methods
- removed job() method in favor of new CronJob(...args) / CronJob.from(argsObject)
- removed time() method in favor of new CronTime()
🛠 Basic Usage
javascript
import { CronJob } from 'cron';
const job = new CronJob(
' ', // cronTime
function () {
console.log('You will see this message every second');
}, // onTick
null, // onComplete
true, // start
'America/Los_Angeles' // timeZone
);
// job.start() is optional here because of the fourth parameter set to true.
javascript
// equivalent job using the "from" static method, providing parameters as an object
const job = CronJob.from({
cronTime: ' ',
onTick: function () {
console.log('You will see this message every second');
},
start: true,
timeZone: 'America/Los_Angeles'
});
Note: In the first example above, the fourth parameter to CronJob() starts the job automatically. If not provided or set to falsy, you must explicitly start the job using job.start().
For more advanced examples, check the examples Top issues
#813"Error: WARNING: Date in past. Will never be fired." crash inside callback wrapper16 comments · 2023-12-09#435support extended cron syntax16 comments · 2019-08-16#967Remove `child_process` dependency for improved browser compatibility1 comments · 2025-02-27#674State of the library & moving forward6 comments · 2023-06-26#1087getNextDateFrom(date, timeZone) returns a date before `date` inside the repeated hour of a fall-back transition (regression in 4.1.1)4 comments · 2026-09-08#671Delayed Jobs with suspend4 comments · 2023-06-01#1049`getNextMatch()` returns past dates near DST boundaries3 comments · 2026-03-08#854Add randomness to execution time3 comments · 2024-02-15
Discussions
#969Question about waitForCompletion1 comments · 2025-03-01#965Use a polling mechanism instead of a look-forward loop3 comments · 2025-02-23#961Replace `CronTime` with `cron-parser`?1 comments · 2025-02-21#960Improving Browser Compatibility2 comments · 2025-02-21#888how could I set up the cron job to run after the previous job completed?2 comments · 2024-07-06#867How can i stop a job when turn off browser0 comments · 2024-03-14#864I can't stop the job1 comments · 2024-03-06#782How to set ID to shedule job1 comments · 2023-11-13
Latest release
v4.4.0 v4.4.0 2025-12-094.4.0 (2025-12-09) ✨ Features update node versions to include 24 and 25 (1031) (b228e7c), closes 1000 ♻️ Chores deps: lock file maintenance (570f9bc) deps: lock file maintenance (1026) (ac05b84)