xarc-run icon indicating copy to clipboard operation
xarc-run copied to clipboard

xrun doesnt support ESM modules

Open kylejeske opened this issue 2 years ago • 0 comments

When trying to use xrun in an environment set to 'Module', xrun will throw an error about using require() instead of import.

[16:17:21] Unable to load ./xrun-tasks.ts: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ./xrun-tasks.ts
require() of ES modules is not supported.
require() of ./xrun-tasks.ts from ./node_modules/.pnpm/@[email protected]/node_modules/@xarc/run/cli/parse-cmd-args.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from ./package.json.

Use Case: Building a TypeScript package that is set to use ESM ('type': 'module') and including xrun tasks for setting how to run Mocha tests causes the xrun tasks to fail.

Solution:

  • parse-cmd-args.js should be updated to use dynamic imports, rather than require

Work Around:

  • Bypass xrun tasks, and run the command directly.
  • TS_NODE_PROJECT='./tsconfig.test.json' NODE_OPTIONS='--loader ts-node/esm --no-warnings' mocha results in a successful pass of the tests.

kylejeske avatar Feb 20 '24 00:02 kylejeske