matrix icon indicating copy to clipboard operation
matrix copied to clipboard

v6.10.7 "The requested module './matrix.js' does not provide an export named 'default'"

Open jdcoldsmith opened this issue 2 years ago • 17 comments

When upgrading from version 6.10.5 to 6.10.7, I am now getting this error in the matrix.mjs file. image image

Here is what my import looks like: image

Any help would be appreciated!

jdcoldsmith avatar Oct 30 '23 20:10 jdcoldsmith

Can you provide a minimal reproduction?

targos avatar Oct 31 '23 07:10 targos

Please try again with v6.10.8. If it still doesn't work, please provide a way to reproduce.

targos avatar Nov 04 '23 09:11 targos

My apologies for not responding sooner! Thank you for your quick response! I will try v6.10.8 and let you know if I run into any issues!

jdcoldsmith avatar Nov 06 '23 14:11 jdcoldsmith

I discovered the issue. We were attempting to upgrade from v6.10.5 to version v6.10.8 and in those changes a change was made to how the package is exported which broke our use of it in a browser context. I had to instruct our dev server to treat ml-matrix as CommonJS instead of ES and we are good to go now.

jdcoldsmith avatar Nov 06 '23 21:11 jdcoldsmith

Would you be able to explain how you use it in a browser context? Maybe we can do something to fix it.

targos avatar Nov 07 '23 09:11 targos

Hi @targos I apologize for the late response but I would like to work through diagnosing and resolving this without a workaround. Currently I am using ml-matrix version 6.10.8 and this is the error that I get when attempted to load our web app. image

The error occurs on this line of the matrix.js file image

I believe the main problem is in the matrix.js file where it uses exports and require which cannot be used in a browser context. I am not sure how much work it would be for you to make this browser friendly, but this would be a really cool addition!

jdcoldsmith avatar Nov 22 '23 18:11 jdcoldsmith

I still don't know how you're using the library in a browser context. Most of our apps are browser-based and work fine.

targos avatar Nov 22 '23 20:11 targos

My apologies for not giving you more information.

We use it in 3 places in our app but here is an example of how we import ml-matrix image

Another thing is that we have "type": "module" set in our package.json. I am not sure if that is an issue.

Thank you for your time helping with this!

jdcoldsmith avatar Nov 22 '23 20:11 jdcoldsmith

Do you have a bundler / dev server ?

targos avatar Nov 22 '23 20:11 targos

Another interesting piece of information is that when building our app using Rollup, we need to use a commonjs plugin specifically for ml-matrix or we get these errors. image And when running the compiled code without the commonjs plugin we get this error: image image

As for our dev server we use Modern Web's @web/dev-server. In order get ml-matrix to work when we serve our code locally we have to use a commonjs plugin and treat ml-matrix as commonjs here as well

jdcoldsmith avatar Nov 22 '23 20:11 jdcoldsmith

This is a really complicated issue. I'm sorry, but I don't see how we can make a fix for your use case without breaking others (recent fixes were related to the default export and CommonJS-ESM interop). I'm happy to accept a patch or any idea.

We need the package to work at least:

  • In Node.js from CommonJS
  • In Node.js from ESM
  • When imported by TypeScript-generated code (CommonJS) with esModuleInterop: true
  • In bundlers such as Vite and Webpack.

targos avatar Nov 29 '23 11:11 targos

Thank you for your time responding to this! I will reach out to some other coworkers to see if we can come up with more ideas or solutions. I am curious, you mentioned that there are others using this package in a browser context. How are they using it that doesn't require them to force the CommonJS version? Are they using Vite or Webpack as their dev server and bundler and that is why it works for them?

jdcoldsmith avatar Nov 29 '23 13:11 jdcoldsmith

Are they using Vite or Webpack as their dev server and bundler and that is why it works for them?

Yes, and to be clear, they are me and my team :)

targos avatar Nov 29 '23 13:11 targos

Awesome! Vite is something that we are interested in potentially transitioning to in the future so I will keep that in mind. I will reach back out after chatting with my team about solutions for getting this to work in our current situation.

jdcoldsmith avatar Nov 29 '23 14:11 jdcoldsmith

The same issue.

lxfu1 avatar Feb 28 '24 12:02 lxfu1

Hi,

Thank you for that package!

I have exact the same issue here: Using ml-matrix in a browser as ES module does not work. Doesn't the extension ".mjs" indicate that this is a ES module and therefore it is an error to import any other type of module there? Why am I using it as a module in the browser? My framework provides an API for developing plugins. Those plugins share various packages, one of them is ml-matrix. If I would bundle all packages into all plugins then the total code size would increase and problems with multiple instances of a single package (most likely in different versions) would pop up.

I would really appreciate if you could solve this issue and make .mjs a real ES module

rebior avatar Nov 18 '24 22:11 rebior

Hello,

from your package.json "module" field I would guess that "src/index.js" is your real ESM entrypoint. As node says that "exports" takes precedence over the other fields: What about adding "import": "src/index.js" to the "exports" filed?

"exports": {
    ".": {
      "types": "./matrix.d.ts",
      "import": "src/index.js",
      "require": "./matrix.js",
      "default": "./matrix.mjs"
    }
  },

keroiber avatar Nov 19 '24 07:11 keroiber