-
I'm trying to use the matrix operations library from ml-matrix in an observable framework notebook for which the new Matrix constructor is returning the following error:
The following code reproduces the error. The first two examples work, showing the import of the library was successful. The third example fails. Testing in node.js and in an online observable notebook the third example is working. import { Matrix } from "npm:ml-matrix";
const mm1 = Matrix.ones(5, 5);
display(mm1);
const mm2 = new Matrix(2,2);
display(mm2);
const mm3 = new Matrix([
[1, 1],
[2, 2],
]);
display(mm3); ml-matrix does have a dependency on is-any-array Any help would be appreciated. Christian |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can fix this by changing how you import ml-matrix like so: import {Matrix} from "npm:ml-matrix/src/index.js/+esm"; The problem is that the default entry point for ml-matrix is matrix.mjs, which imports matrix.js, which is CommonJS generated by rollup. Unfortunately converting ES modules to CommonJS and back again loses information. If you look at the generated matrix.js, it imports is-any-array like this: var isAnyArray = require('is-any-array'); and then references import t from"/npm/[email protected]/+esm"; and import{isAnyArray as t}from"/npm/[email protected]/+esm"; Anyway, I’m sure this is all just annoying noise to you as the user of ml-matrix. But the real answer is that ml-matrix needs to fix its package.json so that it’s distributing valid ES modules, rather than an ES module that imports CommonJS. |
Beta Was this translation helpful? Give feedback.
You can fix this by changing how you import ml-matrix like so:
The problem is that the default entry point for ml-matrix is matrix.mjs, which imports matrix.js, which is CommonJS generated by rollup. Unfortunately converting ES modules to CommonJS and back again loses information. If you look at the generated matrix.js, it imports is-any-array like this:
and then references
isAnyArray.isAnyArray
. So when jsDelivr tries to convert this CommonJS back to an ES module, you get:and
t.isAnyArray
. But heret
is the default export, not a namespace, so you…