Page 1 of 1

Running JavaScript code using the Node.JS interpreter

Posted: Fri Jan 15, 2016 11:05 am
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();

Re: Running JavaScript code using the Node.JS interpreter

Posted: Fri Jan 15, 2016 11:25 am
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 7167 times

Re: Running JavaScript code using the Node.JS interpreter

Posted: Sun Jan 17, 2016 4:39 am
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?

Re: Running JavaScript code using the Node.JS interpreter

Posted: Tue Jan 19, 2016 10:41 am
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 7104 times
With these settings I had no issue running a basic console.log('This is a test.'); file.

Re: Running JavaScript code using the Node.JS interpreter

Posted: Tue Jan 19, 2016 12:54 pm
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.