Fraction.js icon indicating copy to clipboard operation
Fraction.js copied to clipboard

RequireError: unable to load module

Open Martien opened this issue 2 years ago • 7 comments

In Observable notebooks, I use F = require("fraction.js"). However, this now results in RequireError: unable to load module. I can fix it by changing it to F = require('[email protected]/fraction.js').

Is adding the version number mandatory? I’d like to always get the latest version, so not specifying the version number (assuming newer versions are always upwards compatible). Or is that considered unwise and bad practice?

I assume it is related to Native ES module support.

Please advise.

Martien avatar Sep 05 '23 11:09 Martien

Hi Martien, sorry for the problem introduced in your notebook. I don't know Observable, but it seems they support the import syntax as well. Did you try

import F from 'fraction.js';

Nonetheless, it should work with require() as well, if they obey the package.json exports entry.

infusion avatar Sep 05 '23 12:09 infusion

Please see Observable’s notebooks on import:

  • https://observablehq.com/@observablehq/import
  • https://observablehq.com/@observablehq/require

I noticed that this already works fine:

`F = require("[email protected]")`

So, my guess is that something is missing somewhere to make just "fraction.js" refer to the most recent revision?!

Is that something you can/should fix on the JS module side? (I’m a lay person in that area).

Some more confusion: on https://semver.npmjs.com I see that current revision is 4.3.6?! But when I load that version, I get a load error.

Martien avatar Sep 05 '23 14:09 Martien

Yes, the recent version is 4.3.6. Are you able to load 4.3.4? I'd like to understand what the problem here is. Could you please paste the load error here?

As far as I understand, require gives you access to npm while import allows you to import code hosted on Observable. The question is, how does Observable read the package.json. They give an example with https://www.npmjs.com/package/simple-statistics which also uses exports, so it should work with Fraction.js 4.3.6 the same.

infusion avatar Sep 06 '23 06:09 infusion

When opening the console and trying to load fraction.js @4.3.6 a MIME error occurs. Seems Observable is not sending the right MIME header for .cjs files.

infusion avatar Sep 06 '23 07:09 infusion

For wat it is worth, please see https://observablehq.com/@martien/getting-require-fraction-js-to-work

Version number mismatch between package.json and fraction.cjs may cause the error?

// try uncomment each require turn by turn and reload the page // search Console » Sources for "fraction" // examine package.json.version and fraction.cjs

// Fraction = require("fraction.js@4") // unable to load module; // package.json shows 4.3.6 // fraction.js shows 4.3.5

Fraction = require("[email protected]") // works fine // package.json shows 4.0.14 // fraction.js shows 4.0.14

// Fraction = require("fraction.js") // unable to load module; // package.json shows 4.3.6 // fraction.cjs shows 4.3.5 (31/08/2023)?!

// Fraction = require("[email protected]") // unable to load module; // package.json shows 4.3.6 // fraction.cjs shows 4.3.5 (31/08/2023)

// Fraction = require("[email protected]") // unable to load module; // package.json shows 4.3.6 // fraction.cjs shows 4.3.5 (31/08/2023)

Martien avatar Sep 07 '23 07:09 Martien

Hi @infusion, any progress on this? [Shhhh, Shhhh. It's, oh, so quiet – Björk]

Martien avatar Sep 12 '23 13:09 Martien

The release is coming today :)

infusion avatar Sep 12 '23 13:09 infusion