unjs / jiti

Runtime TypeScript and ESM support for Node.js
MIT License
1.79k stars 62 forks source link

Importing `moment-timezone` from ESM context fails with `require() of ES Module latest.json is not supported` #314

Closed ldiqual closed 2 weeks ago

ldiqual commented 2 weeks ago

Environment

Node.js v20.17.0 jiti: 2.0.0 moment-timezone: 0.5.45

Reproduction

test.ts

import 'moment-timezone'

Run with:

JITI_DEBUG=1 node --import jiti/register test.ts

Describe the bug

Jiti fails to import moment-timezone with error:

Error [ERR_REQUIRE_ESM]: require() of ES Module file:///path/to/node_modules/moment-timezone/data/packed/latest.json not supported.

Additional context

No response

Logs

[jiti] [init] version: 2.0.0 module-cache: true fs-cache: true interop-defaults: false
[jiti] [cache] [hit] ./scripts/test.ts ~> ./node_modules/.cache/jiti/scripts-test.903402b1.mjs
[jiti] [init] version: 2.0.0 module-cache: true fs-cache: true interop-defaults: false
[jiti] [native] [import] /path/to/node_modules/moment-timezone/index.js
[jiti] [cache] [hit] /path/to/node_modules/moment-timezone/index.js ~> ./node_modules/.cache/jiti/moment-timezone-index.4501337a.mjs
node:internal/modules/esm/loader:566
      throw new ERR_REQUIRE_ESM(url, true);
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module file:///path/to/node_modules/moment-timezone/data/packed/latest.json not supported.
    at Object.<anonymous> (/path/to/node_modules/moment-timezone/index.js:2:16) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.17.0
pi0 commented 2 weeks ago

Thanks for report. With 2.1 we enabled interopDefault by default (https://github.com/unjs/jiti/pull/310) which resolves this.

Added test https://github.com/unjs/jiti/commit/1d86ca3a68cb5af1563cc27d328c72ec0e5c22b5#diff-19e56682197c4354febf4c924009c8ddc2d2d1bc33f281d083b1f4ad8c1b3919R1