Using NodeJS in Mathematica 12

Written by on June 8, 2019 in Mathematica, Programming with 0 Comments

In an earlier article, I had described Python integration in Mathematica 12. In addition to Python, NodeJS is also supported as a default “external” language. In today’s article, I will focus on NodeJS integration. By the way, NodeJS support was introduced in Mathematica 11.2.

Before using NodeJS with Mathematica 12, the system must be configured appropriately. The steps are clearly explained here. It is quite simple and straightforward.

To confirm that Mathematica can now interact with NodeJS, run the following command:

FindExternalEvaluators Function

FindExternalEvaluators Function

The response shows that everything is in order.

Using NodeJS Cell

Let us begin with the NodeJS cell. Type “>” in the Mathematica notebook and it converts the current cell into Python cell. 

Python Cell

Python Cell

To convert this into a NodeJS cell, left-click on the Python symbol and then select NodeJS option.

Selecting NodeJS Cell Type

Selecting NodeJS Cell Type

Now you can enter NodeJS expressions and evaluate them.

NodeJS Expression

NodeJS Expression

Here is some more:

NodeJS Expression

NodeJS Expression

Using ExternalEvaluate[]

The other option is to use “ExternalEvaluate[]” as follows:

ExternalEvaluate Function

ExternalEvaluate Function

The first argument denotes the language to use, in this case, “NodeJS”. Keep in mind that each call to ExternalEvaluate[] is evaluated in the context of a temporary NodeJS session.

Explicit NodeJS Session

Often times, it is convenient to create an explicit NodeJS session and evaluate expressions in that session, finally terminating the session when no more needed.

Explicit NodeJS Session

Explicit NodeJS Session

We can also load a NodeJS file and use any function from that file.

NodeJS Session Continued

NodeJS Session Continued

We are ready to close the session at this point.

Closing the Session

Closing the Session

Support for NodeJS appears to be quite mature. It will come in handy when we use Mathematica to communicate with external channels that support NodeJS.

This concludes today’s discussion on using NodeJS in Mathematica. Feel free to download my Mathematica notebook and NodeJS file.

Have a nice weekend!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top