Load mathjs in a browser module (ES import)
See original GitHub issueCurrently it looks like it’s not possible to use mathjs in a browser module like:
<script type="module">
import { create, all } from 'mathjs'
</script>
I think the existing bundles in dist
do not have the right exports to be consumed as an ES module.
What should work (but doesn’t) is loading like:
import { create, all} from 'mathjs/main/es/index.js'
I think the reason is that many import paths in the code are missing the explicit “*.js” extension, which is the normal way to go in nodejs and webpack environments, but not in browser ES environments which require a real path to a file.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Get started - Math.js
ES modules #. Load the functions you need and use them: import { sqrt } from ...
Read more >mathjs - npm
Math.js is an extensive math library for JavaScript and Node.js. ... The code of mathjs is written in ES modules, and requires all...
Read more >how to include and use math.js - Stack Overflow
Import import { create, all } from 'mathjs'.
Read more >import - JavaScript - MDN Web Docs - Mozilla
To load modules in non-module contexts, use the dynamic import syntax instead. import declarations are designed to be syntactically rigid (for ...
Read more >JavaScript - Parcel
This example imports a multiply function from math.js , and uses it to implement ... Both the ES module import statement and CommonJS...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s quite complicated :S
mathjs has no global state, you have to create your own mathjs instance with for example
create(all, config)
, and in this instance there is state like the config. It may be possible that some difficulties arise because we integrate for exampleComplex
andBigNumber
, I’m not entirely true if mathjs adds methods to these libraries.I think you’re correct about the lack of file extension; browser environments expect the full path and filename. Is there a way you could tell Gulp to add the
.js
to imports when it created the files?