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 script start message to a node.script object and stop the process by sending the message script stop.

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 script start.

To pass arguments to the Node process on startup, append those arguments to the script start message. For example, the message script start config1.json 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 process.argv. The first two arguments will always be:

  1. The path to the Node executable
  2. 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

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 @watch attribute of the node.script object to enable restarting the Node script automatically if the node.script object detects a change to its source file.

Similarly, you can use the @autostart attribute of the node.script object to start the Node script automatically when the patch is opened.

Keep in mind that using either of these attributes never sends script start to the node.script object, so you cannot send startup arguments to the node.script object in the usual way. Instead, the @args attribute can be used to send arguments to the Node script, which will be accessible via process.argv.

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:

Note: Any arguments that you express using the @args attribute will only be available in conjunction with the @watch or @autostart attributes. Otherwise, arguments that follow a script start message will override the arguments specified with @args.

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