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:
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.
To convert this into a NodeJS cell, left-click on the Python symbol and then select NodeJS option.
Now you can enter NodeJS expressions and evaluate them.
Here is some more:
Using ExternalEvaluate[]
The other option is to use “ExternalEvaluate[]” as follows:
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.
We can also load a NodeJS file and use any function from that file.
We are ready to close the session at this point.
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!
Recent Comments