{"skill":{"slug":"node-cron","displayName":"Node Cron","summary":"Node.js cron job scheduling with the `cron` npm package. Use when the user needs to schedule recurring tasks, create cron jobs, validate cron expressions, se...","description":"---\r\nname: node-cron\r\ndescription: Node.js cron job scheduling with the `cron` npm package. Use when the user needs to schedule recurring tasks, create cron jobs, validate cron expressions, set up timed callbacks, or work with cron syntax in a Node.js/TypeScript project. \r\n---\r\n\r\n# Node Cron\r\n\r\nUse the `cron` npm package (`npm install cron`) for second-precision scheduled tasks in Node.js.\r\n\r\n## Triggers\r\n\r\ncron job, node-cron, schedule task, cron expression, cron pattern, scheduled job, npm install cron, CronJob, CronTime.\r\n\r\n## Quick Start\r\n\r\n```ts\r\nimport { CronJob } from 'cron';\r\n\r\nconst job = new CronJob(\r\n  '0 */5 * * * *',  // every 5 minutes\r\n  () => console.log('Running every 5 minutes'),\r\n  null,             // onComplete\r\n  true,             // start immediately\r\n  'Asia/Shanghai'   // timeZone\r\n);\r\n```\r\n\r\nOr use the object form:\r\n\r\n```ts\r\nconst job = CronJob.from({\r\n  cronTime: '0 0 9 * * 1',    // 9am every Monday\r\n  onTick: () => sendReport(),\r\n  start: true,\r\n  timeZone: 'Asia/Shanghai'\r\n});\r\n\r\njob.stop();   // halt\r\njob.start();  // resume\r\n```\r\n\r\n## Cron Patterns\r\n\r\nThe library uses **6 fields** (second-precision), unlike standard 5-field Unix cron:\r\n\r\n```\r\nsecond  minute  hour  day-of-month  month  day-of-week\r\n  0-59   0-59   0-23     1-31       1-12     0-7\r\n```\r\n\r\n### Syntax\r\n\r\n| Token | Meaning |\r\n|-------|---------|\r\n| `*` | Any value (every second/minute/etc.) |\r\n| `1,3,5` | List of values |\r\n| `1-5` | Range (inclusive) |\r\n| `*/5` | Every N steps |\r\n\r\n### Names\r\n\r\nUse first 3 letters for month/day-of-week (case-insensitive):\r\n`\"jan,mar,dec\"`, `\"mon,wed,fri\"`\r\n\r\nDay-of-week: `0` or `7` is Sunday.\r\n\r\n### Common Examples\r\n\r\n| Expression | Meaning |\r\n|------------|---------|\r\n| `*/10 * * * * *` | Every 10 seconds |\r\n| `0 * * * * *` | Every minute on the second |\r\n| `0 0 * * * *` | Every hour |\r\n| `0 0 9 * * *` | Daily at 9:00 AM |\r\n| `0 30 9 * * 1-5` | 9:30 AM Mon-Fri |\r\n| `0 0 0 1 * *` | Midnight on the 1st of every month |\r\n\r\n## CronJob Class\r\n\r\n### Constructor Parameters\r\n\r\n| Param | Required | Description |\r\n|-------|----------|-------------|\r\n| `cronTime` | [OK] | Cron pattern string, `Date`, or Luxon `DateTime` |\r\n| `onTick` | [OK] | Callback function |\r\n| `onComplete` | | Called when job stops via `job.stop()` |\r\n| `start` | | Auto-start. Default `false` |\r\n| `timeZone` | | IANA zone string (e.g. `'Asia/Shanghai'`) |\r\n| `context` | | `this` context for `onTick` |\r\n| `runOnInit` | | Fire `onTick` immediately on init |\r\n| `waitForCompletion` | | If `true`, skip ticks while callback is still running |\r\n| `errorHandler` | | Catch exceptions in `onTick` |\r\n| `name` | | Job identifier for debugging |\r\n| `threshold` | | Missed-deadline tolerance in ms. Default `250` |\r\n\r\nDo NOT pass `utcOffset` together with `timeZone`  --  they conflict.\r\n\r\n### Key Methods\r\n\r\n- **`CronJob.from(obj)`** (static)  --  Create with named params\r\n- **`job.start()` / `job.stop()`**  --  Start/stop\r\n- **`job.nextDate()`**  --  Next execution as Luxon DateTime\r\n- **`job.nextDates(n)`**  --  Array of next N dates\r\n- **`job.lastDate()`**  --  Last execution date\r\n- **`job.setTime(cronTime)`**  --  Change schedule\r\n- **`job.addCallback(fn)`**  --  Add another onTick callback\r\n\r\n### Read-Only Properties\r\n\r\n- **`job.isActive`**  --  Whether job is running\r\n- **`job.isCallbackRunning`**  --  Whether onTick is currently executing\r\n\r\n## Standalone Utilities\r\n\r\n```ts\r\nimport * as cron from 'cron';\r\n\r\n// When will this cron expression fire next?\r\nconst dt = cron.sendAt('0 0 * * *');\r\nconsole.log(dt.toISO());\r\n\r\n// How many ms until next execution?\r\nconst ms = cron.timeout('0 0 * * *');\r\n\r\n// Validate an expression\r\nconst { valid, error } = cron.validateCronExpression('0 0 * * *');\r\n```\r\n\r\n## Gotchas\r\n\r\n- **Month is 1-12**, not 0-11. Upgrade from v2 needs `+1` on all month values.\r\n- **Day-of-week 0 = Sunday** (7 also works).\r\n- **6 fields** (with seconds) -- unlike standard 5-field Unix cron.\r\n- Use `waitForCompletion: true` for async callbacks to prevent overlap.\r\n\r\n## Async onTick\r\n\r\nWrap async work directly  --  `waitForCompletion` prevents overlapping runs:\r\n\r\n```ts\r\nconst job = CronJob.from({\r\n  cronTime: '*/30 * * * * *',\r\n  onTick: async () => {\r\n    await fetchData();\r\n    await processData();\r\n  },\r\n  waitForCompletion: true,\r\n  start: true\r\n});\r\n```\r\n\r\n## Full API Reference\r\n\r\nSee [references/api_reference.md](references/api_reference.md) for the complete API documentation including `CronTime` class, all method signatures, and migration notes.\r\n","tags":{"latest":"1.0.0"},"stats":{"comments":0,"downloads":320,"installsAllTime":0,"installsCurrent":0,"stars":0,"versions":1},"createdAt":1778040496206,"updatedAt":1778492854429},"latestVersion":{"version":"1.0.0","createdAt":1778040496206,"changelog":"Initial release of node-cron skill.\n\n- Provides concise guidance for scheduling cron jobs in Node.js using the `cron` npm package.\n- Documents six-field (second-precision) cron patterns and syntax.\n- Details the CronJob class, including constructor parameters, methods, and properties.\n- Includes common scheduling examples and recommended usage patterns.\n- Offers utility functions for validating cron expressions and calculating next run times.\n- Notes differences from standard Unix cron and important migration gotchas.","license":"MIT-0"},"metadata":null,"owner":{"handle":"openlark","userId":"s1727wv2g20pc729snzcm4nf8183hy72","displayName":"OpenLark","image":"https://avatars.githubusercontent.com/u/260858787?v=4"},"moderation":null}