How do you run JavaScript script through the Terminal?

ID : 10373

viewed : 18

Tags : javascriptterminaljavascript

Top 5 Answer for How do you run JavaScript script through the Terminal?

vote vote

92

Another answer would be the NodeJS!

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Using terminal you will be able to start it using node command.

$ node > 2 + 4 6 >  

Note: If you want to exit just type

.exit 

You can also run a JavaScript file like this:

node file.js 

« Install it NOW »

vote vote

90

If you have a Mac you can get jsc a javascript console in OS X (Terminal) by typing

/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc 

in Terminal.app.

You could also run one of your .js script by adding its name as an argument for jsc, like this:

jsc your_awesome_script_name.js 

Notice: I use console.log() during development but jsc needs the debug() function instead.

On Ubuntu you have some nice ECMAScript shells at your disposal. Between them it's worth to mention SpiderMonkey. You can add It by sudo apt-get install spidermonkey

On Windows as other people said you can rely on cscript and wscript directly built on the OS.

I would add also another :) way of thinking to the problem, if you have time and like to learn new things i'd like to mention coffee-script that has its own compiler/console and gives you super-correct Javascript out. You can try it also on your browser (link "try coffeescript").

UPDATE July 2021: You can also install and use the brilliant QuickJS which on OS X could be installed via brew install quickjs. Then an interactive console will be available at your propmt with qjs

vote vote

70

You would need a JavaScript engine (such as Mozilla's Rhino) in order to evaluate the script - exactly as you do for Python, though the latter ships with the standard distribution.

If you have Rhino (or alternative) installed and on your path, then running JS can indeed be as simple as

> rhino filename.js 

It's worth noting though that while JavaScript is simply a language in its own right, a lot of particular scripts assume that they'll be executing in a browser-like environment - and so try to access global variables such as location.href, and create output by appending DOM objects rather than calling print.

If you've got hold of a script which was written for a web page, you may need to wrap or modify it somewhat to allow it to accept arguments from stdin and write to stdout. (I believe Rhino has a mode to emulate standard browser global vars which helps a lot, though I can't find the docs for this now.)

vote vote

64

Use node.js for that, here is example how to install node by using brew on mac:

brew update && install node 

Then run your program by typing node filename.js, and you can use console.log() for output.

vote vote

59

If you're using MacBook.

  1. Set up node.js in your system and open up the terminal
  2. Navigate to the directory, where the js file is saved.
  3. To execute run node <filename.js>

example, if filename is script.js run node script.js

Top 3 video Explaining How do you run JavaScript script through the Terminal?

Related QUESTION?