Project

Profile

Help

saxon-js 2.6.0 causes "Must use import to load ES Module"

Added by Karim Ratib about 1 year ago

When I upgrade saxon-js to 2.6.0, I encounter the following error in my module-based node.js project:

Must use import to load ES Module: [..]/node_modules/axios/index.js

  at Runtime.requireModule (node_modules/jest-runtime/build/index.js:972:21)
  at node_modules/saxon-js/SaxonJS2N.js:4646:74
  at Object.<anonymous> (node_modules/saxon-js/SaxonJS2N.js:4655:112)

If I downgrade back to 2.5.0, the error disappears.

I'm using this syntax to import the library:

import SaxonJS from 'saxon-js'

As far as I can tell, the dependency axios jumped from 0.24.0 to 1.6.1, possibly causing this ES module incompatibility because the offending line changed from

module.exports = require('./lib/axios');

to

import axios from './lib/axios.js';

but the code inside node_modules/saxon-js/SaxonJS2N.js still uses require().


Replies (2)

RE: saxon-js 2.6.0 causes "Must use import to load ES Module" - Added by Karim Ratib about 1 year ago

Oops, looks like I spoke too soon. This seems related to Jest settings (I am import SaxonJS in unit tests). I will update this thread when I find out more.

RE: saxon-js 2.6.0 causes "Must use import to load ES Module" - Added by Karim Ratib about 1 year ago

I was able to fix this dependency error by instructing Jest to use the axios CommonJS module instead of the ESM module that it finds by default. Here's the relevant commit: https://github.com/infojunkie/musicxml-midi/commit/fd9c32cb4fa54610e70b3c098833a127d488faff

The idea is to override the Jest module mapping via moduleNameMapper to point the module name axios to a locally-made CommonJS module that explicitly references the CommonJS module inside the axios package. The downside is that we need to explicitly refer to a specific file inside the axios package instead of relying on the information provided by package.json. It would seem that Jest doesn't follow the expected module resolution logic, hence this hack.

Hope this helps someone!

    (1-2/2)

    Please register to reply