JS require not working for Math.js
Hi there,
I'd like to use the Math.js library inside Max's js object. This should work using the require() function in my script. I've read the "JS Require Guide" in the documentation, and can get that example to work, but when I then try to require Math.js (in the same directory as my patch and js file), I get an error.
Math.js is a regular Node.js library, available also through npm. From what I can tell, it uses the "exports" property to expose its functions and should be compatible with Max's js object.
Any help on getting this to work would be immensely appreciated! I've attached a simple example patch that should print the square root of -4 (2i) to the Max window, but only outputs an error message for me.
Hey Jonas.
quick sidenote: Unfortunately your test/example setup lacked the js_require_test.js file and also hiding the toolbar and sidebar makes it kinda hard to debug.
Anyhow, I took a stab at this and it does seem to work just fine if setting up for the fact that Math.js is providing a namespace when using it via Max' require.
In order to adjust your js for that try something like this:
var math = require("math.min.js").math;
function bang() {
post(math.round(math.e, 3));
post(math.atan2(3, -3));
post(math.sqrt(-4));
}
Thanks Florian! This was the mistake - I hadvar math = require("math.min.js");
instead of
var math = require("math.min.js").math;
Now it works as expected. Thank you!