Mathjs: ERROR TypeError: Cannot convert 2546 to type "undefined"; valid output types are number, BigNumber, Fraction

Created on 16 Jul 2019  路  24Comments  路  Source: josdejong/mathjs

Hello,
I'm trying to use the evaluate() method to do some math using a string in an Angular application. This is my method:

calculateMileageCharge() {
    let formula = this.worksheet.mileageChargeRef;

    formula = formula.replace('${firstMileage}', this.worksheet.firstMileage.toString();
    formula = formula.replace('${purPrice}', this.mainForm.get('purPrice').value);

    this.worksheet.mileageChargeRefCal = evaluate(formula);
  }

The formula var works out to be: "2546 * (9988 / 120000)"
but any formula (i.e. 2*3) throws the same error with the first number in the formula.

This all works fine while developing, but as soon as I use ng build --prod i get the error in the title. Building without --prod works fine.

This is my package.json, in case it helps. The prod build uses the "build:prod" script:

{
  "name": "bbms",
  "version": "0.2.0",
  "scripts": {
    "ng": "ng",
    "postinstall": "node version.js",
    "start": "ng serve -c localapi",
    "start:devapi": "ng serve -c devapi",
    "start:localapi": "ng serve -c localapi",
    "build": "ng build",
    "build:prod": "npm version minor --no-git-tag-version && node version.js && ng build --prod  --baseHref=/bbms/ --deploy-url=/bbms/resources/bbms/",
    "build:prodMajor": "npm version major --no-git-tag-version && node version.js && ng build --prod  --baseHref=/bbms/ --deploy-url=/bbms/resources/bbms/",
    "build:prodPatch": "npm version patch --no-git-tag-version && node version.js && ng build --prod  --baseHref=/bbms/ --deploy-url=/bbms/resources/bbms/",
    "build:prodNoVer": "ng build --prod  --baseHref=/bbms/ --deploy-url=/bbms/resources/bbms/",
    "build:eclipse": "ng build --configuration=eclipse --output-path=../webapp/resources/bbms --baseHref=/bbms/ --deploy-url=/bbms/resources/bbms/",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.2.2",
    "@angular/cdk": "^7.2.2",
    "@angular/common": "^7.2.10",
    "@angular/compiler": "^7.2.10",
    "@angular/core": "^7.2.10",
    "@angular/flex-layout": "^7.0.0-beta.24",
    "@angular/forms": "~7.2.2",
    "@angular/material": "^7.2.2",
    "@angular/platform-browser": "~7.2.2",
    "@angular/platform-browser-dynamic": "~7.2.2",
    "@angular/router": "~7.2.2",
    "@fortawesome/angular-fontawesome": "^0.3.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.12",
    "@fortawesome/free-solid-svg-icons": "^5.6.3",
    "@types/mathjs": "^6.0.0",
    "angular-file": "^1.2.2",
    "angular-in-memory-web-api": "^0.8.0",
    "core-js": "^2.5.4",
    "fs-extra": "^8.1.0",
    "google-libphonenumber": "^3.2.2",
    "hammerjs": "^2.0.8",
    "jquery": "^3.4.1",
    "libphonenumber-js": "^1.7.15",
    "mathjs": "^6.0.3",
    "ng2-file-upload": "^1.3.0",
    "ngx-infinite-scroll": "^7.1.0",
    "ngx-spinner": "^7.1.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.12.4",
    "@angular/cli": "^7.3.6",
    "@angular/compiler-cli": "^7.2.10",
    "@angular/language-service": "~7.2.2",
    "@ryaneharris/bbms-schematics": "^0.1.1",
    "@types/google-libphonenumber": "^7.4.17",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.29",
    "@types/node": "~10.12.18",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~3.3.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~8.0.1",
    "tslint": "~5.12.1",
    "typescript": "~3.2.4"
  }
}

Let me know if you need more info.

Thanks!

Most helpful comment

The same issue here with angular. On localhost, before build it's working, but after ng build --prod, I have the same error message.
@Update - ShiftyMcCool
I downgrade to 5.10.3 and @types to 5.0.1 and it's working. Thank you.

All 24 comments

How can I reproduce your issue? Can you isolate the issue? Did you do debugging of the contents of formula right before you pass it to evaluate (both dev and prod)?

Debugging showed the contents of the formula as 2546 * (9988 / 120000) before passing to evaluate() but, like I mentioned, even something as simple as 2*3 was failing (hard-coded). I'm not sure this can be duplicated in something like StackBlitz since it is only a problem after ng build --prod. The only way to reproduce, as far as I can tell, is to start an Angular project with my package.json and try it.

EDIT: Looks like downgrading to mathjs 5.10.3 / @types/mathjs 5.0.1 and using eval() instead of evaluate() is working correctly in dev and prod

I haven't used Angular for years. Would it be easy for you to set up a small github repo with (a minimal version of) your angular project so I can check out, start it, and see what's going on?

Sure, I might need a couple days though, I'm on a tight deadline for this project and will be able to breathe pretty soon. I'll post back with the URL when it's ready.

The same issue here with angular. On localhost, before build it's working, but after ng build --prod, I have the same error message.
@Update - ShiftyMcCool
I downgrade to 5.10.3 and @types to 5.0.1 and it's working. Thank you.

@KrystianKatafoni
Thanks for the solution. I had same error and after changes as you mentioned its working now.
But I want to know why the latest version not working on production build? Any idea?

Thanks

Any update on this issue?

Any update on this issue?

No. What would be helpful is a minimal code example demonstrating the issue. I'm not sure how to reproduce.

Sorry for the delay, lots of tight projects lately. Minimal reproduction here: https://stackblitz.com/edit/angular-8rfnpw

Works fine in the browser, but download the code and do a production build. After the project is built, serve the contents of the dist folder and you'll see this in the console:

image

EDIT: Tried with the most recent versions of both mathjs and @types/mathjs with the same result: https://stackblitz.com/edit/angular-4u6npy

Thanks for reporting back Ryan!

I downloaded https://stackblitz.com/edit/angular-8rfnpw, run npm install. Tried npm start which works fine, and also npm run build and then opening dist/demo/index.html works fine.

How can I get the issue to trigger exactly?

It has to be a production build, use ng build --prod

Ah ok, thanks, I can now reproduce the issue. No clue what could be the cause though.

I have the same issue... @josdejong did you find a solution ?

I haven't yet found time to look more deeply into this (except being able to reproduce the issue).

Help would be very welcome!

thanks, I have the same issue.

This appears to still be an issue. Downgrading to the following fixes the issue but I'm surprised the latest version isn't compatible with a prod build? I have to assume the evaluate command has a conflict since build using eval works fine?

"mathjs": "5.10.3"
"@types/mathjs": "5.0.1"

@markamccorkle [email protected] contains a _huge_ refactor and breaking changes, see changelog.

Again, any help figuring this out would be very welcome :)

@josdejong I was upgrading to Angular 9 and forgot about this issue until I noticed mathjs updated to 6.6.0 and @types/mathjs to 6.0.4. On a whim I decided to give it a shot and it seems to be working now with the mathjs.evaluate() method on a prod build. Not sure if it was the Angular upgrade or recent mathjs / @types/mathjs changes but something seems to have fixed it.

@markamccorkle @xcorpio @KrystianKatafoni @lovechaudhary @ShadabFaiz @izanagi1995 can any of you confirm the fix either on Angular 9 or an older version?

That's good news Ryan, thanks for the feedback!

I want to test with a clean angular 9 application but I have some issues getting something running at all.

Tried again but now with success: created an empty angular 9 application, added mathjs, build with ng build --prod, and things just work :+1: :+1: :+1:

Will close this issue now.

Hi,
I'm on angular 7 and using mathjs version 7. I'm running in this same issue with angular production build. Is there any patch i could apply as a temporary fix? the last resort would be to downgrade.

I'd love to contribute to help troubleshoot. I haven't navigated through the codebase yet but i plan to some time this week.

I'm not aware of any workarounds. It is fixed in angular 9 though, is there a possibility to upgrade your application? (or at least try out if that would solve the issue).

It would be great if you could find a fix for older versions of angular, but it would be a pity to spend huge amounts of time in an issue which is solved in the latest version of angular.

sadly, we're working on a pretty large angular 7 project. Upgrading to angular 9 is pretty far in our roadmap.

You're right, It's unreasonable to expect a fix for an older version, especially if it's not trivial as in this case.

I will try investigate and I hope the fix is small. For now, I downgraded to 5.10

Thank you for your prompt reply :)

馃憤 if it turns out to be a small fix that would be awesome. Thanks for your effort!

Was this page helpful?
0 / 5 - 0 ratings