How do you run JavaScript script through the Terminal?

JavascriptTerminal

Javascript Problem Overview


For instance, if you were to run a Python script you would type python filename.py or if you wanted to run a C program make filename then ./ filename. How do you do this with .js files?

Javascript Solutions


Solution 1 - Javascript

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 »

Solution 2 - Javascript

If you have MacOS you can get jsc a javascript console by typing on Terminal.app:

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

On older versions of OS X, the jsc command is located at:

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

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

Solution 3 - Javascript

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.)

Solution 4 - Javascript

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.

Solution 5 - Javascript

It is crude, but you can open up the Javascript console in Chrome (Ctrl+Shift+J) and paste the text contents of the *.js file and hit Enter.

Solution 6 - Javascript

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

Solution 7 - Javascript

Alternatively, if you're just looking to play around with Javascript a nice in browser option is http://labs.codecademy.com/">Codecademy's</a> Javascript Lab.

They also have a Python and Ruby IDE.

Solution 8 - Javascript

If you are on a Windows PC, you can use WScript.exe or CScript.exe

Just keep in mind that you are not in a browser environment, so stuff like document.write or anything that relies on the window object will not work, like window.alert. Instead, you can call WScript.Echo to output stuff to the prompt.

http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx

Solution 9 - Javascript

I tried researching that too but instead ended up using jsconsole.com by Remy Sharp (he also created jsbin.com). I'm running on Ubuntu 12.10 so I had to create a special icon but if you're on Windows and use Chrome simply go to Tools>Create Application Shortcuts (note this doesn't work very well, or at all in my case, on Ubuntu). This site works very like the Mac jsc console: actually it has some cool features too (like loading libraries/code from a URL) that I guess jsc does not.

Hope this helps.

Solution 10 - Javascript

You need installed JS engine like Node, then use a shebang line in very first line of your file, like this:

script.js

#!/usr/bin/env node
console.log('Hello terminal');

after that you must set executable permission:

chmod +x script.js

And run it

./script.js

Solution 11 - Javascript

On Ubuntu, install a link to install libjavascriptcoregtk-3.0-bin and use /usr/bin/jsc (manpage).

Solution 12 - Javascript

This is a "roundabout" solution but you could use ipython

Start ipython notebook from terminal:

$ ipython notebook

It will open in a browser where you can run the javascript

enter image description here

Solution 13 - Javascript

All the answers above are great, I see one thing missing and could be considered for running javascripts(*.js) files, the unrelated brother of javascript the Java.

JDK comes up with two nice tools, could be utilized for executing javascripts. Here are command goes like. Make sure to navigate to JDK\bin.

 jjs example.js

Its comes up with another commmand tool that goes like this-

 jrunscript example.js

I hope this may be helpful to others.

Solution 14 - Javascript

All you have to do to run a js file via bash is type: $ node filename.js

This is similar to in python, when you do: $ python filename.py

Solution 15 - Javascript

You can also use phantomjs Download phantomjs depending on the system (my case is Max OSX) from phantomjs.org .You should put the path to phantomjs installation folder on the top of your javascript file. eg. #!./bin/phantomjs Save your code. Go to the terminal where your javascript is saved and you can run using > phantomjs filename.js

Solution 16 - Javascript

Technically, Node.js isn't proper JavaScript as we know it, since there isn't a Document Object Model (DOM). For instance, JavaScript scripts that run in the browser will not work. At all. The solution would be to run JavaScript with a headless browser. Fortunately there is a project still active: Mozilla Firefox has a headless mode.

https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode

$ /Applications/Firefox.app/Contents/MacOS/firefox -headless index.html
*** You are running in headless mode.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBLUCView Question on Stackoverflow
Solution 1 - JavascriptIonică BizăuView Answer on Stackoverflow
Solution 2 - JavascriptmicrospinoView Answer on Stackoverflow
Solution 3 - JavascriptAndrzej DoyleView Answer on Stackoverflow
Solution 4 - JavascriptAndrey BodoevView Answer on Stackoverflow
Solution 5 - JavascriptMark LakataView Answer on Stackoverflow
Solution 6 - JavascriptEmjeyView Answer on Stackoverflow
Solution 7 - JavascriptfscofView Answer on Stackoverflow
Solution 8 - JavascriptAnders Marzi TornbladView Answer on Stackoverflow
Solution 9 - Javascriptuser2005477View Answer on Stackoverflow
Solution 10 - JavascriptapfliegerView Answer on Stackoverflow
Solution 11 - JavascriptKeith CascioView Answer on Stackoverflow
Solution 12 - JavascriptAlexView Answer on Stackoverflow
Solution 13 - JavascriptRed BoyView Answer on Stackoverflow
Solution 14 - Javascriptstevestar888View Answer on Stackoverflow
Solution 15 - JavascriptewalelView Answer on Stackoverflow
Solution 16 - JavascriptredolentView Answer on Stackoverflow