The module uses inconsistent export syntax
The module is using exports.default with exports.__esModule, but it's not setting module.exports directly. This is causing a behavior difference between Node.js and most bundlers when using ESM.
import cron from 'cron-validate';
// This works in Node, but fails in most bundlers
cron.default('').isValid();
// This works in bundlers, but throws 'TypeError: cron is not a function' when
// run in Node (and also causes TypeScript to complain)
cron('').isValid();
For more details see:
- https://arethetypeswrong.github.io/?p=cron-validate%401.5.2
- https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSOnlyExportsDefault.md
For more context, version 1.4.5 set both exports.default and module.exports correctly (but had errors in the types). Not sure exactly what changed between that version and latest (1.5.2), but hopefully that helps the investigation.
I'm having a similar issue with v1.5.2 while using Vite.
-
Calling
cron.default('')is throwingTypeError: cron is not a function. -
And this is the TypeScript error when calling
cron(''):
This expression is not callable.
Type 'typeof import("node_modules/.pnpm/[email protected]/node_modules/cron-validate/lib/index")' has no call signatures.ts(2349)
As a work-around, use the syntax in the second example above ( cron('')), and simply @ts-ignore the line.
A workaround for ESM:
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const cronValidate = require('cron-validate').default
Another workaround:
import cron from 'cron-validate';
// For TypeScript
const cronFn = (cron.default ?? cron) as typeof cron.default;
// For pure JS
const cronFn = cron.default ?? cron;