Running JavaScript code using the Node.JS interpreter

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
rodriguennr
Posts: 5
Last visit: Fri Jul 06, 2018 6:46 am

Running JavaScript code using the Node.JS interpreter

Post by rodriguennr »

PrimalScript 2015, v7.1.74, 64bit
Operating system: Windows 10-64bit

I am wondering whether it is possible to run standalone JavaScript codes in PrimalScript and output the result to the PrimalScript "Output" window. For example, I want to run the following code and I need to use the Node.js or any other interpreter except the Microsoft JScript runtime. How do I set this up so that the result of the script is displayed on the PrimalScript "Output" window?

Many thanks in advance.
  1. var player1={name:"Fred", score:"1000", rank:20};
  2. var player2={name:"Sam", score:"30000", rank:3};
  3.  
  4. function playerDetails(){
  5.   console.log(this.name + " has a rank of " + this.rank +
  6.               " and a score of " +this.score)
  7. }
  8.  
  9. player1.logDetails=playerDetails;
  10. player2.logDetails=playerDetails;
  11.  
  12. player1.logDetails();
  13. player2.logDetails();
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Running JavaScript code using the Node.JS interpreter

Post by DevinL »

You can use a custom interpreter by navigating to Options -> Environment -> Languages and selecting JavaScript. Off to the right you'll see a section for the Script Interpreter:
Custom_Interpreter.png
Custom_Interpreter.png (26.88 KiB) Viewed 7168 times
DevinL
SAPIEN Technologies, Inc.
User avatar
rodriguennr
Posts: 5
Last visit: Fri Jul 06, 2018 6:46 am

Re: Running JavaScript code using the Node.JS interpreter

Post by rodriguennr »

Ok, I should have mentioned that I had already tried this suggestion but it still doesn't work. PrimalScript still picks the Microsoft JScript interpreter. This then fails to execute any valid JavaScript code successfully.

I have tried various arguments and Initial Directory paths to no avail. I have tried the following arguments
$file$
node $file$
$filename$ along with $filedir$

Executing my script from the Windows command line using the Node.JS interpreter works without issue as seen in the below screenshot.

It must be a simple setting somewhere but I have been able to figure this out. Please help! Anyone?
Attachments
CommandLine_NodeJS.png
CommandLine_NodeJS.png (28.17 KiB) Viewed 7146 times
JavaScript_Interpreter.png
JavaScript_Interpreter.png (262.97 KiB) Viewed 7146 times
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Running JavaScript code using the Node.JS interpreter

Post by DevinL »

I'm sorry about that, I said JavaScript but JScript has the .js extension associated to it. The easiest way to fix this is to instead modify the JScript settings like so:
2016-01-19_10-38-13.png
2016-01-19_10-38-13.png (28.11 KiB) Viewed 7105 times
With these settings I had no issue running a basic console.log('This is a test.'); file.
DevinL
SAPIEN Technologies, Inc.
User avatar
rodriguennr
Posts: 5
Last visit: Fri Jul 06, 2018 6:46 am

Re: Running JavaScript code using the Node.JS interpreter

Post by rodriguennr »

Perfect! This works now with the above settings. In fact I went into the JScript options and disassociated the JS extension from JScript and back to the JavaScript options, I have associated the JS extension with JavaScript. Thank you.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.