Billboard fails to load as an ES6 module
See original GitHub issueDescription
I’m trying to use billboard as a dependency in a dynamically loaded module, which tries to load billboard with:
import './billboard.js';
But this fails with the error TypeError: Cannot read properties of undefined (reading 'd3')
. Debugging tells me that the issue is due to this
being undefined in a module. The this
usage at billboard.js 3.3.3 line 16 seems to be meant to be the window object.
As the module doing the import is dynamically loaded, I cannot add the billboard script tag to the html.
I’ve also tried the billboard.pkgd.js
script, resulting in TypeError: Cannot set properties of undefined (setting 'bb')
. Debugging shows a similar setup as in the billboard.js
script, trying to use this
as the window
object.
Is there a way to use billboard as an import in this way? Or can you modify the script to use the window
object?
Steps to check or reproduce
<script type="module" src="some-module.js"></script>
// some-module.js
import './d3.min.js';
import './billboard.js'; // fails
// do something with billboard
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
I was not yet aware of import-maps, thanks for the pointer! I’ll have to experiment a bit and see how this matches our browser compatibility requirements.
This might be an interesting point to add to the readme.
The ESM distribution’s dependencies imports are pointed as “module-name”. When you check the billboard.js’ ESM dist file, all the necessary dependencies imports are defined at the top as:
So, to execute it needs load those imports first. As you pointed, to be imported directly to the browser (w/o transpilation), the “module-name” location should be relative or some origin where browser could identify.
If you aren’t aware on browser compatibility, you can use import maps to fix that.
And here’s working demo: