Possible to include JavaScript file into another?
Hi,
I would like to put some JavaScript functions I use often in a separate file to prevent copy/paste. Is it somehow possible to include a JavaScript file into another? Or is there a workaround?
Cheers!
Hi !
well . you can include js content by putting your Utils into Cycling '74/jsextensions/ folder .
This way your functions are seen within every JS project used in max .
Personally i would love to have #include directive as an option and be specific to the current project, but thats not possible without writing a compiler for JS (someone did) ,
every changes require recompiling .
There is third option . Pack your utils into Global object inside one JS instance and access it in another .
mostly im using the last one .
Thanks for the tip with the Global. Works fine!
Excuse me, but I'm not sure to understand that third solution about global object :/ someone can explain ?
U are able to setup a global object in one JS instance, that holds your methods/variables/objects and access them inside other JS instances .
But in Max 7 weve got new directives https://cycling74.com/forums/js-require/ that let us concatinate functionality .
It's very unfortunate that the implementation of JS require
is wrong - or at least, it was wrong in the first release of Max 7 (I haven't upgraded, partly for that reason, partly because I have shows coming up and want to wait until there's a break with some time to kill before potentially destabilizing my world. I hear good things about Max 7 but experience with software in general leads me to caution...)
The issue is that you can't refer to files in subdirectories - even though the Common JS spec requires it. It's unfortunate, because most third-party Common JS packages do use subdirectories in their require
sections, rendering them incompatible with Max/MSP's JS implementation.
I do hope this gets fixed and I will upgrade to Max 7 the moment it is... hint hint hint! :-) There was some argument last time this came up as to whether this was even a bug but I think the Common JS specification is authoritative in this matter...
OK :) thank!
Thanks @TOM SWIRLY ! its good to be aware before jumping in . Indeed i would be surprised because i would expect it to behave as its origin .
by the way : Wanst it you who wrote compiler for JS that let us concatinate files ?
Heh, well, I wrote the examples of this, but it actually uses gcc and make, standard tools that are on pretty well every machine!
I'd love to stop having that phase - if nothing else to avoid the error where I change the program and forget to compile them - and perhaps when Max 7 JS handles relative paths I can!
By the way, my Max style has completely changed now - my patches have almost no wires in them! Instead I have a single Javascript box that everything connects to. Within that JS box is all my logic - plus I directly find the Max boxes and use the message method to send data right them. I also have a way to automatically connect inlet to object method calls so I just have to change a table to change my inputs.
It makes my life much easier when I'm not dragging wires to land on tiny inlets... :-)
Its not popular approach here :) its against Max'ing attitude :D . Since dealing with the coding myself , im patching less for similar reasons , desires and efficiency . But we wont be applaud for this here , its sort of a sin not to wire stuff TOM :)))))))))
A sin! :-o ;-)
Well, "Better to reign in Hell, than to serve in Heaven." Though I'm pretty sure Satan invented the computer mouse...
you mean Douglas Engelbart ? you ingrate :DDDDD
Hi, has there been an update for this?
I know that there is the require function now, but after trying with a module I want to use it just says that `.load` is not a function...
I'm trying to use libraries and modules from NPM...