Starting and stopping a process, @watch and @autostart
Starting a Node process with Arguments
Each node.script object manages a single Node process, which runs alongside the Max application. You start a Node process by sending the message to a node.script object and stop the process by sending the message .
There are times when you want to pass arguments to the Node process as it starts. For example, you might be working with a web-server script that displays different content depending on one of several configuration files it loads. Here, you might want to pass the name of the configuration file as an argument to
.To pass arguments to the Node process on startup, append those arguments to the
message. For example, the message would pass the string "config1.json" to the Node script as it starts.Any arguments sent to the Node process will be available in the global variable
. The first two arguments will always be:- The path to the Node executable
- The path to the running JavaScript file.
Any arguments beyond those two will be the ones that you passed to the Node script.
For more information on the global variable process.argv see https://nodejs.org/docs/latest/api/process.html#process_process_argv
The @autostart, @watch and @args attributes
You may find that it becomes tiresome to have to constantly restart the node.script object each time you edit and make changes to the script in the node.script object.
You can use the node.script object to enable restarting the Node script automatically if the node.script object detects a change to its source file.
attribute of the
Similarly, you can use the node.script object to start the Node script automatically when the patch is opened.
attribute of the
Keep in mind that using either of these attributes never sends node.script object, so you cannot send startup arguments to the node.script object in the usual way. Instead, the attribute can be used to send arguments to the Node script, which will be accessible via process.argv.
to the
The image shown above will set “arg1” and “arg2” as the first two arguments to the maxnode-basic.js program. In order to use these from within the JavaScript context you can use the following code:
// Slice process.argv in order to get the actual arguments because
// in NodeJS the first two values have the following content:
//
// process.argv[0] - is the path of the Node executable used
// process.argv[1] - is the path of the JS program that is executed
const args = process.argv.slice(2)
For more information on process.argv see the Node JS API Documentation: https://nodejs.org/docs/latest-v20.x/api/process.html#processargv
Note: Any arguments that you express using the
attribute will only be available in conjunction with the or attributes. Otherwise, arguments that follow a message will override the arguments specified with .
See Also
Name | Description |
---|---|
Node for Max Documentation - Table of Contents | Node for Max Documentation - Table of Contents |
Node for Max - Script Lifecycle | Node for Max - Script Lifecycle |
node.script |