1

I am using Angular 8 and math.js library for big number manipulation.

I just have an updated version of math.js from 5.4.0 to 6.2.3.

I am using math.js in my component in this way:

import * as mathjs from 'mathjs';

constructor() {
   mathjs.config({ number: 'BigNumber', precision: 128 });
}

Suddenly new error appears after the update.

Error: The global config is readonly. Please create a mathjs instance if you want to change the default configuration. Example:

import { create, all } from 'mathjs';
const mathjs = create(all);

mathjs.config({ number: 'BigNumber' });

I tried import { create, all } from 'mathjs, but those methods do not exist at all.

What is the workaround for this problem?

Rifky Niyas
  • 1,737
  • 10
  • 25
Milos
  • 1,678
  • 4
  • 23
  • 49

2 Answers2

0

Not sure if solve your problem, But as per the error you should create a new instance and use later like below -

import { create, all } from 'mathjs';
  const mathjs = new create(all);
  mathjs.config({ number: 'BigNumber' });
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • Yeas, I tried this but since I did not updated @types/mathjs I got errors. Now I have 2 problems with line const mathjs = new create(all); 1) Shadowed name: 'mathjs' (no-shadowed-variable) 2) (property) create: (factories: FactoryFunctionMap, config: ConfigOptions) => Partial import create Expected 2 arguments, but got 1.ts(2554) – Milos Nov 07 '19 at 10:40
0

After some time I finally found solution.

First I needed to remove line:

import * as mathjs from 'mathjs';

This line does not make seance anymore since we need to create variable with this name which will be new instance of mathjs with new configuration.

import { create, all, MathJsStatic } from 'mathjs';

private mathjs: Partial<MathJsStatic>;

  constructor() {
    this.mathjs = create(all, { number: 'BigNumber', precision: 128 });
  }

If we need the same configuration of mathjs over all the application, the good way is to create a service and use the same instance everywhere.

Milos
  • 1,678
  • 4
  • 23
  • 49