"NodeJS" (External Evaluation System)

Details

  • Node.js Version 7.10.1 and higher is supported.
  • Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.
  • To configure Node.js for use in the Wolfram Language, follow the instructions from the Configure NodeJS for ExternalEvaluate workflow.

ExternalEvaluate Usage

  • ExternalEvaluate["NodeJS",code] executes the code string in a Node.js REPL and returns the results as a Wolfram Language expression.
  • ExternalEvaluate["NodeJS""String",code] executes the code string in a Node.js REPL and does not interpret the results.

Data Types

Examples

open allclose all

Basic Examples  (2)

Use the File wrapper to execute code contained in a file:

Deploy code using CloudDeploy and then run the code directly from a CloudObject:

Use a URL wrapper to directly run code hosted online:

Scope  (20)

Start a session:

Evaluate a Boolean statement in NodeJS and return it:

Concatenate strings in NodeJS and return the result:

Create an ExternalFunction with NodeJS:

Call the function:

Close the session:

Session Options  (9)

"ReturnType"  (3)

For NodeJS, the default return type is "Expression":

Numbers, strings, lists and associations are automatically imported for the "Expression" return type:

The return type of "String" returns a string, using JSON:

"Version"  (1)

You can use "Version" to make sure only a particular version of the evaluator is used:

You can specify a minor or patch version:

"Evaluator"  (1)

Evaluate code using a specified "Evaluator":

"SessionProlog"  (1)

Use "SessionProlog" to perform a side effect at the start of a session:

"SessionEpilog"  (1)

Use "SessionEpilog" to perform a side effect at the end of a session:

"Prolog"  (1)

Use "Prolog" to perform a side effect before every evaluation:

"Epilog"  (1)

Use "Epilog" to perform a side effect after every evaluation:

Command Options  (10)

"Command"  (4)

When a string is provided, the command is directly executed:

The above is equivalent to writing the command using this form:

Use a File wrapper to run the code from a file:

The above is equivalent to writing the command using this form:

Use a URL wrapper to directly run code hosted online:

The above is equivalent to writing the command using this form:

Put code in a CloudObject:

Evaluate directly from the cloud:

The above is equivalent to writing the command using this form:

"ReturnType"  (1)

Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:

"Arguments"  (2)

Use "Arguments" to call the command with arguments:

For a single argument, you do not need to use a list:

If you need to pass a list as the first argument, wrap it with an extra list explicitly:

You can name a function in "Command" and directly call it with "Arguments":

The same result can be archived by using a Rule:

An alternative method is to define an ExternalFunction:

"Constants"  (1)

Use "Constants" to set global variables before the command runs:

"TemplateArguments"  (2)

When running a command, you can inline a TemplateExpression:

You can explicitly fill TemplateSlot using "TemplateArguments":

If you need to pass a list as the first argument, wrap it with an extra list explicitly:

You can name template slots and use an Association to pass named arguments to the template:

Applications  (2)

Define the Range function in NodeJS:

Use the function:

Use prettier to reformat a string of JavaScript code:

Possible Issues  (1)

Running a command that directly returns an object will return a syntax error:

To return an object, you need to add parentheses: