JS require still isn't CommonJS compatible - doesn't understand paths!
Jun 20 2015 | 8:42 pm
I complained about this several months ago but I just bought Max 7... so... :-)
There are four issues with the Javascript require command in Max 7, three of which can be gotten around, but one of which can't - and makes writing production code hard.
1. require doesn't understand paths.
Here is where paths are discussed in the CommonJS specification. Paths are not an optional part of the spec! :-) Here are tests that include tests for absolute and relative paths.
It is impossible to get around this restriction in any production-quality fashion. The solution "put all code in the same subdirectory" is simply a non-starter for me - I have 147 separate JS files in over a dozen subdirectories. More, I was hoping to start to introduce some well-known JS packages like lodash into my project - but the correct way to do that is with a git subtree or git submodule - i.e. as a subdirectory.
My current Max 6 solution (use make) will continue to work, but is lame for several reasons - I have to run a make after each time I change anything, and it leaves these big ".jso" files, the result of the make with all the .js glued together, in my subdirectories - it's also not suitable for using third-party libraries that expect to use exports.
2. The js box can't find Javascript files in a subdirectory of its current directory.
This is actually a regression from Max 6! Before you could have a box looking like js sub/foo.js and it'd work. Now it doesn't, whether or not you use a full absolute path or a relative path.
3. You don't get an error if require fails to find the file.
Instead, strangely enough, require returns some sort of function that I can't identify and seems to do nothing if called. It'd be easy to detect this in your code using the JS typeof operator.
4. Your Javascript only gets recompiled if the top .js file changes, not if any files it requires are changed.
You could easily workaround 2, 3 or 4 if 1 worked...
Here is a repo demonstrating these issues.