JS Require
In the Max 7 release notes it mentions 'js require implementation' and also 'buffer~: exposed to JS', is there any information about these?
Max now supports two new functions for having JS files which use other JS files for functionality without requiring them to be in the jsextensions folder and universally loaded by all JS objects:
1. var exports = require(filename) which works like CommonJS 1.0
For example with the following in a file called require-example.js
// for require, the module must be defined by assigning anything
// the module wishes to export as properties of an "exports" property
// This exports property is the return value from the require function.
// modules included using require are always scoped to prevent collisions
// in the top level script. Currently we don't support any of the other
// typcial node.js module gobal properties like module.id, module.loaded, etc.
exports.foo = 37;
exports.bar = function () {
post("I'm executing bar\n");
}
This may then be included and used in another js file as the following:
var reqmodule = require("require-example");
reqmodule.bar();
post("foo is "+reqmodule.foo+"\n");
2. include(filename) which simply includes the javascript at the topmost level of the javascript file which calls this. This is more dangerous practice than the contained module definition of CommonJS and require(), but is useful in many situations
example attached
jinx
Wow, you guys are fast. These should get me going. Thanks!
Hi I was hoping require would resolve using Max's file preferences. Instead the file has to be relative to the requiring scripts directory. Can you confirm this?
ie I've put a js file in one of the folders listed in Max and I can't require it's file name.
@ORANGE_GLASS I believe you have to give it an absolute path, or a path relative to the script that is requiring it.
OK thanks Ben for confirming.