Running A JScript Script With Node.JS
If you find yourself needing to execute a JScript script with Node.js then this post is for you. Why would you want to do this? Well, read on.
JScript vs JavaScript
JScript and JavaScript are different dialects of the ECMAScript standard. With JScript you can do things with WMI, use ActiveXObject constructors and some syntax features not present in JavaScript.
You Can’t Write A Node.JS App In JScript
I tried below code snippet that is meant to print “Hello World!” in a window and run it with the node
command.
However I get the following error.
Clearly, you can’t write JScript code for a Node.js application.
But There Is A Way!
In order to execute a JScript script, you can use the cscript.exe found in C:\Windows\System32 directory. So, for my Node.js example application, I am using the child_process core library to execute the JScript code.
The first line is to find the path to cscript.exe and then creating a child process to use the cscript.exe to run the JScript code. Now you can use the node
command to run the Node.js application to execute the JScript code in sample.js.
And here is the result.
I hope you found this post useful. You can find the code snippet here.