Back

CreateNode

JS Py
Hello World
Content:
- Methods

Creates a NodeJS background process with all the functionality of common node.js.

node = app.CreateNode( paths?, options? ) → app object: Node

Note that to define private functions (functions that are invisible by the main app) you have to declare them as global variable without usign the var keyword.
This is actually a safer way of working as it prevents name clashes and libraries overwriting each other's functions and variables accidentally.  Ideally you should put your code into objects or classes for better protection and avoid using many globals.

var myLocalVariable = "Local Hello";
myGlobalVariable = "Global Hello"

function myLocalFunction() { return "Local Hello"; }
myGlobalFunction = function() { return "Global Hello"; }

For more details have a look at the Node docs in the Plugins page

Example - Basic

var nodeJs = 'console.log("Hello World");\nconsole.error("Hello Error");\n'

function OnStart()
{
    app.WriteFile("node_script.js", nodeJs);

    lay = app.CreateLayout("linear", "VCenter, FillXY")
    txt = app.AddText(lay, "Debug Log:", 1, 1, "Log");

    node = app.CreateNode();
    node.SetOnOutput((msg) => { txt.Log(msg); });
    node.SetOnError((msg) => { txt.Log(msg, "red"); });
    node.SetOnMessage((msg) => { txt.Log(msg); });
    node.SetOnReady(node_OnReady);


    app.AddLayout(lay);
}

function node_OnReady()
{
    node.Run("node_script.js");
}
from native import app

nodeJs = 'print("Hello World");\nconsole.error("Hello Error");\n'

def OnStart():
    global node
    app.WriteFile("node_script.js", nodeJs)

    lay = app.CreateLayout("linear", "VCenter, FillXY")
    txt = app.AddText(lay, "Debug Log:", 1, 1, "Log")

    node = app.CreateNode()

    def onOutput(msg):
        txt.Log(msg)

    def onError(msg):
        txt.Log(msg, "red")

    def onMessage(msg):
        txt.Log(msg)

    node.SetOnOutput(onOutput)
    node.SetOnError(onError)
    node.SetOnMessage(onMessage)
    node.SetOnReady(node_OnReady)

    app.AddLayout(lay)

def node_OnReady():
    node.Run("node_script.js")
    Copy     Copy All       Run      

Methods

The following methods are available on the Node object:

Execute( js, id? )
GetEnv( name ) → String
GetVersion() → Number
IsDone() → Boolean
IsReady() → Boolean
Boolean
List
String
String: javascript code
String: separated
String: path to file or folder ( “/absolute/...” or “relative/...” )
String: path to file or folder ( “/absolute/...” or “relative/...” ): “NODE_PATH env variable”
String: comma “,” separated: extended, legacy, nostart, esm
String: “name” or “name^ver”
String: optional target directory
String: Context ID
List: argument array
function()
function( msg )
function( stdout )
node.AddModule
Install a node module from npmjs.org
node.CloseMsgPipe
Close a message pipe
node.Execute
Execute a line of code in the node process.
node.GetEnv
Get a process environment variable.
node.GetVersion
Returns the plugin version
node.IsDone
Returns if the node process exited
node.IsReady
Returns if the Node component is ready for use
node.OpenMsgPipe
Open a message pipe
node.Run
Run a NodeJS source file. Use id to run in a new context
node.SendMessage
Send a message to the running node process. Calls cb of parent.SetOnMessage
node.SendPipeMsg
Send a message over the message pipe
node.SetEnv
Set a process environment variable.
node.SetOnDone
Define a callback function which is called when the node process has exited.
node.SetOnError
Define a callback function which is called when the node process prints to stderr.
node.SetOnMessage
Define a callback function which is called when a system/pipe message was received from the node process.
node.SetOnOutput
Define a callback function which is called when the node process prints to stdout.
node.SetOnReady
Define a callback function which is called when the Node component is ready for use.
node.Start
Start the main Node process