Registry / devops / cron-parser

cron-parser

JSON →
library2.0.0jsnpmunverified

cron-parser is a robust Node.js library for parsing, validating, and iterating over cron expressions. It currently stands at stable version 5.5.0 and demonstrates a consistent release cadence with frequent updates addressing bug fixes, performance improvements, and new features like the `H` (hashed value) syntax. Key differentiators include comprehensive timezone support, intelligent handling of Daylight Saving Time (DST), and an iterator-based API for easily generating future schedule dates. It supports both standard cron formats and several special characters like `L` (last day), `#` (nth day of week), and `H` (randomized values), alongside predefined expressions such as `@daily` and `@hourly`. The library is ESM-first and requires Node.js >= 18 and TypeScript >= 5.

npm install cron-parser
INSTALL
IMPORT
SIG · CRON-PARSER
C
cron-parser
devopsjavascriptv2.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseExpression
✓ import { parseExpression } from 'cron-parser';
✗ const { parseExpression } = require('cron-parser');
The library is primarily designed for ES Modules. While CommonJS `require` might work in some setups, native ESM import is the recommended approach for Node.js >= 18.
CronExpression
✓ import { CronExpression } from 'cron-parser';
✗ import CronExpression from 'cron-parser';
CronExpression is a named export, not a default export.
CronOptions
✓ import type { CronOptions } from 'cron-parser';
Importing types requires the `type` keyword in TypeScript for clarity and to prevent bundling issues, especially in mixed ESM/CJS environments.

This quickstart demonstrates how to parse a cron expression, specify timezone options, and iterate through the next scheduled dates using the `parseExpression` function and `CronExpression` iterator.

import { parseExpression, CronExpression } from 'cron-parser'; async function getNextScheduledTimes(cronExpression: string, count: number = 5): Promise<Date[]> { try { const options: CronOptions = { currentDate: new Date(), tz: 'America/New_York', // Specify a timezone, e.g., process.env.TZ ?? 'UTC' iterator: true }; const interval: CronExpression = parseExpression(cronExpression, options); const nextDates: Date[] = []; for (let i = 0; i < count; i++) { try { const next = interval.next(); nextDates.push(next.value.toDate()); } catch (e) { console.warn(`Could not get next date ${i + 1}:`, (e as Error).message); break; } } return nextDates; } catch (err) { console.error(`Error parsing cron expression '${cronExpression}':`, (err as Error).message); return []; } } // Example usage: Every minute, between 9 AM and 5 PM, Monday to Friday in New York const cronString = '0 */1 9-17 * * 1-5'; getNextScheduledTimes(cronString, 3).then(dates => { console.log(`Next 3 occurrences for '${cronString}':`); dates.forEach(date => console.log(date.toISOString())); }); // Example with predefined expression getNextScheduledTimes('@daily', 2).then(dates => { console.log(`\nNext 2 occurrences for '@daily':`); dates.forEach(date => console.log(date.toISOString())); });
Debug
Known issues
breakingThe `CronField` constructor signature changed and the `nthDayOfWeek` field was removed from `CronExpressionOptions`. This impacts direct manipulation of cron field objects.
fix
Review the pull request #379 for detailed changes. If directly constructing `CronField` objects, adapt to the new constructor signature. If using `nthDayOfWeek` in options, find an alternative approach or ensure your cron expression implicitly handles it.
affects: >=5.2.0
gotchaPrior to v5.0.5, `cron-parser` would set the `currentDate` to UTC by default if no timezone was specified, which could lead to unexpected results when running in a local timezone. This was reverted to use the current system timezone.
fix
For versions before 5.0.5, always explicitly specify the `tz` option in `CronOptions` if you need a specific timezone, or update to v5.0.5 or later to use the system's local timezone by default for `currentDate`.
affects: <5.0.5
gotchaWhen using the `L` (last day of month) special character, specific issues with handling explicit months have been fixed in v5.1.1. Incorrect dates could be returned for certain last-day scenarios.
fix
Ensure you are using `cron-parser` version 5.1.1 or newer to benefit from the fix for last day of month handling when an explicit month is set.
affects: <5.1.1
gotchaInvalid start and end time span validation logic issues existed in versions prior to 5.3.1, potentially leading to incorrect range parsing or unexpected errors when defining expression spans.
fix
Update to version 5.3.1 or newer to ensure correct validation and parsing of cron expressions involving start and end date spans.
affects: <5.3.1
Errors
Common errors & fixes
Error: Not a valid CRON expression
The provided cron string does not conform to the expected format or contains invalid characters/ranges.
fix
Double-check the cron expression against the documentation for valid fields, ranges, and special characters. Ensure all required fields (minute, hour, day of month, month, day of week, and optional second) are present and correctly formatted.
TypeError: require is not defined in ES module scope
Attempting to use CommonJS `require()` syntax in an ES Module context (`type: 'module'` in package.json or `.mjs` file).
fix
Change `const { parseExpression } = require('cron-parser');` to `import { parseExpression } from 'cron-parser';`. Ensure your environment supports ES Modules or transpile your code if necessary.
Error: Cannot find module 'cron-parser' or its corresponding type declarations.
The `cron-parser` package is not installed or not correctly recognized by your module resolution system (e.g., TypeScript or Node.js).
fix
Run `npm install cron-parser` or `yarn add cron-parser`. For TypeScript, ensure `esModuleInterop` is `true` in your `tsconfig.json` if encountering issues with default/named imports, though `cron-parser` provides types.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
luxonrequiredInternal dependency for date/time manipulation and timezone handling.
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
1
Resources
cron-parser — npm install cron-parser · libregistry