Search code examples
javascriptangularmathjs

Mathjs 'The global config is read-only'?


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?


Solution

  • 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.