question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load mathjs in a browser module (ES import)

See original GitHub issue

Currently 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
josdejongcommented, May 12, 2020

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 example Complex and BigNumber, I’m not entirely true if mathjs adds methods to these libraries.

0reactions
GreenImpcommented, Jul 28, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found