The `cron` library for Node.js provides a robust solution for scheduling tasks (functions or external commands) using the familiar cron syntax, with support for second-level precision. The current stable version is 4.4.0, demonstrating active development with regular patch and minor releases, alongside beta releases for upcoming major versions like v5. Key differentiators include its flexibility in defining schedules using cron strings, standard JavaScript `Date` objects, or `Luxon DateTime` objects, comprehensive TypeScript support, and features for managing job lifecycle (start, stop, handling unexpected termination). It is designed for long-running Node.js applications and has evolved through major versions, introducing stricter cron pattern adherence and API refinements.
npm install cronVerified import paths — ran on the pinned version, not inferred.
Schedules a function to run every second using a cron string and demonstrates basic job creation and immediate start.
Upgrade your Node.js installation to version 18 or higher. For example, using nvm: `nvm install 18 && nvm use 18`.
Use `job.start()` to begin a job and `job.stop()` to halt it. Check `job.isActive` for the current status.
When migrating from v2 to v3, adjust all numeric month values by incrementing them by 1. Ensure day-of-week patterns account for `0` or `7` representing Sunday.
Rewrite `new CronJob({ ...args })` to `new CronJob(cronTime, onTick, onComplete, start, timeZone, ...)` or use `CronJob.from({ cronTime, onTick, ... })`.Users who relied on string-based command execution should migrate to manually executing commands within their `onTick` function using Node.js's native `child_process` API directly.
Upgrade to `cron` v4.3.4 or higher to ensure errors in async `onTick` functions are properly caught. Implement robust `try...catch` blocks within your async `onTick` handlers for older versions.
Upgrade your Node.js environment to version 18 or higher. Use `nvm install 18 && nvm use 18` or similar version management tools.
Use `job.start()` to initiate the job and `job.stop()` to pause it. The job's active status can be checked via `job.isActive` (read-only).
Adjust your cron patterns to use 1-12 for months (1=January, 12=December) and 0-7 for day-of-week (0 or 7=Sunday) as per v3 changes.
For CommonJS, use `const { CronJob } = require('cron');`. For ESM, use `import { CronJob } from 'cron';`. Ensure it's a named import.Ensure you are on `cron` v3 or newer to use `CronJob.from()`. Verify `CronJob` is correctly imported and available.