1

I have a simple JavaScript myScript.js with code as below;

var printer = function(someString){
    console.log(someString);
}

printer("This is printed on console.");

I'm on Windows 10 and need to be able to call myScript.js on a command prompt and have the code executed without doing node myScript.js. Is there a way to set up things so that Command prompt or PowerShell can automatically call Nodejs or other JavaScript engine?

3
  • You could install node.js for that yes. But if it's just to test some code, just open an empty browser window and play around with the console there. Commented Apr 14, 2017 at 11:19
  • See stackoverflow.com/questions/6818031/… Commented Apr 14, 2017 at 11:21
  • I guess associating a file extension default will do that. Commented Apr 14, 2017 at 11:21

1 Answer 1

2

If your Windows can run normal shell scripts on the command line then you can add a shebang line to your Node script just like you do with shell scripts. For example:

#!/usr/bin/env node
// your node code here

Also you can try to configure Node to be invoked for all files with the .js extension but this can be a security hazard because you may execute arbitrary code just by clocking on JavaScript files that you may have downloaded on your system so I wouldn't really recommend that.

Another alternative is to make a BAT script for you Node app:

example.bat:

node example.js

example.js:

// your Node script

That you will be able to run with just:

example

on your command line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.