cron-validate icon indicating copy to clipboard operation
cron-validate copied to clipboard

The module uses inconsistent export syntax

Open simon-abbott opened this issue 10 months ago • 3 comments

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

simon-abbott avatar Apr 14 '25 16:04 simon-abbott

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.

simon-abbott avatar Apr 14 '25 16:04 simon-abbott

I'm having a similar issue with v1.5.2 while using Vite.

  1. Calling cron.default('') is throwing TypeError: cron is not a function.

  2. 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.

mcinerneyjake avatar Apr 29 '25 01:04 mcinerneyjake

A workaround for ESM:

import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const cronValidate = require('cron-validate').default

jamcalli avatar Jun 17 '25 21:06 jamcalli

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;

simon-abbott avatar Nov 20 '25 00:11 simon-abbott