A newer version of Max is available. Click here to access the latest version of the Max documentation

Node for Max - ECMAScript Modules

Usint ECMAScript modules with node.script

As of Max v8.6, in addition to scripts (using the .js extension), node.script also supports ECMAScript Modules (ESM) and associated features like top-level await. The easiest way to load an ES module is using the .mjs extension for the file you'll be passing as the script argument to node.script

Please refer to the Node.JS documentation to learn more about ECMAScript modules and their interoperability with CommonJS modules.

node.script lifecycle with top-level await

With the introduction of support for using the await syntax in ECMAScript modules at the top-level of your code the asynchronous API of the max-api module can be used in the global scope. Given that all inter-process communication has been set up prior to loading your module all API method calls, incl. outlet and post will work as expected.

In order to understand the implication of that on the lifecyle messages of node.script let's assume the following code in your entrypoint module:

import { post } from "max-api"

// Simple sleep utility function to simulate an asynchronous task
const sleep = time => new Promise(resolve => setTimeout(resolve, time))

await post("Starting async task")
await sleep(1000)
await post("Finished async task")
When sending a script start message to the node.script object you will see the following sequence of events:
  1. A start success message out of rightmost outlet to signal that the process has started successfully
  2. A loadstart success message out of rightmost outlet to signal that the process is about to load the module
  3. node.script will post Starting async task to the Max Console.
  4. About 1 second later node.script will post Finished async task to the Max Console.
  5. A loadend success message out of rightmost outlet to signal that the process has successfully loaded the module
For a practical example of the above please refer to the "loadend" tab in the node.script helpfile.

See Also

Name Description
Node for Max Documentation - Table of Contents Node for Max Documentation - Table of Contents
node.script