Mathjs: SyntaxError: Unexpected token 'export' in Jest environment using `mathjs/number`

Created on 11 Mar 2020  ยท  4Comments  ยท  Source: josdejong/mathjs

Error Description

I use [email protected] in my application

import { median, mode } from 'mathjs/number';

When execute Jest test, this error was displayed.

  โ— Test suite failed to run

    /path/to/project/node_modules/mathjs/number.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './main/esm/number' // eslint-disable-line
                                                                                             ^^^^^^

    SyntaxError: Unexpected token 'export'

Suggestion

In mathjs/index.js using module.exports

https://github.com/josdejong/mathjs/blob/86606dc7bedf742e7d6af1423a99552ec0711139/index.js#L5

But mathjs/number.js using export * from '' different from mathjs/index.js

https://github.com/josdejong/mathjs/blob/86606dc7bedf742e7d6af1423a99552ec0711139/number.js#L1

So, I change the mathjs/number.js, Jest test was passed.

- export * from './main/esm/number' // eslint-disable-line
+ module.exports = require('./lib/entry/mainNumber') 
bug help wanted

All 4 comments

Thanks for reporting, this is a good point and is currently inconsistent. We should be careful to change this, that may break existing usage.

for the main library mathjs, we have an automatic switch in package.json to select either commonjs or ESM:

{
  "main": "main/es5",
  "module": "main/esm"
}

I'm not sure if it is possible to have this switch working for mathjs/number too - right now this only points to the ESM version.

Any ideas?

btw you can always use the right import by choosing from:

  • mathjs/main/es5
  • mathjs/main/es5/number
  • mathjs/main/esm
  • mathjs/main/esm/number

Anyone able to figure out the consequences and think through a solution? Help would be very welcome.

I mentioned this briefly on #1928 but this problem may be solvable using Subpath Exports. I've not used them before, but the documentation looks promising.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skgadi picture skgadi  ยท  4Comments

SamuelMarks picture SamuelMarks  ยท  3Comments

piotr-s-brainhub picture piotr-s-brainhub  ยท  3Comments

skgadi picture skgadi  ยท  3Comments

nioate picture nioate  ยท  4Comments