Unable to import my own library: TopoHelper

I’m having a hard time loading a module. It’s topohelper a new library I made to help manipulate topojson with ease.
I tried to use the module debugger without success.
I finally found a way with the new esm.run from jsdelivr, but I would really appreciate some help on why it cannot be loaded with require.
See this notebook that summarize this: Import topohelper / Thomas Ansart / Observable

Thanks!

The file that contains the UMD module (which we need for require) has the file ending .cjs (for CommonJS, indicating a Node module), which causes it to be served with the Content-Type header “application/node”. You need to rename it to “.js” so that it gets served with the correct MIME type.

In the meantime you can use this workaround:

topohelper = require(URL.createObjectURL(new Blob([
  await (await fetch('https://cdn.jsdelivr.net/npm/topohelper@0.4.0/dist/topohelper.umd.cjs')).text()
], {type: 'text/javascript'})))

What we do above is to fetch the file contents, then create a Blob with the correct MIME type from it, and from that create an object URL which we pass to require().

3 Likes

:tada:
Thanks to you, I have a temporary solution and you also gave me a big clue to solve it for good. I now force vite to build module with good extension file.
I now pass the module debugger stress test with success.
:pray: