317

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?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
BLUC
  • 2,590
  • 2
  • 15
  • 24
  • 3
    @BLUC It's called an executable. – helpermethod Dec 16 '11 at 10:44
  • executable, application, program, or binary. And on a more helpful note, you'll need a JavaScript interpreter, like V8 or Rhino, or one of the other 1000 ones I'm forgetting. – Corbin Dec 16 '11 at 10:45
  • 10
    @OliverWeiler it's not as easily applicable in general situations as some other languages, but I do believe it can have it's applications outside of web pages :). – Corbin Dec 16 '11 at 10:46
  • An application of JavaScript is in Unity 3D – Bradman175 Mar 05 '17 at 13:00

16 Answers16

463

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 »

loki
  • 187
  • 2
  • 14
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • @NadirSampaoli Yes, it's a new technology that it's really great! – Ionică Bizău Jul 15 '13 at 16:16
  • The only downside (if you want to call it that) is that node is always on "strict mode". Is it correct? – Nadir Sampaoli Jul 15 '13 at 16:38
  • @NadirSampaoli Hmmm, I am not sure. See this question: http://stackoverflow.com/q/9031888/1420197 – Ionică Bizău Jul 15 '13 at 17:27
  • 12
    @NadirSampaoli I doubt that it counts as a "downside". Strict mode is better for everyone. – The Paramagnetic Croissant Jul 02 '14 at 10:05
  • Any advice on wrapping a javascript into final ‘consumer ’command, the user doesn't have to care about? As in `$> sudo apt-get install myFancyCmd` followed by `$>myFancyCmd hello.jpg -w 1234`? – Frank N Oct 25 '16 at 02:54
  • Just a note that you can install Node via Homebrew by issuing this terminal command: `brew install node` – jamesnotjim Sep 16 '17 at 14:47
  • On Ubuntu 14, `node` is [Ham Radio](http://tldp.org/HOWTO/AX25-HOWTO/), not JavaScript. So try the Ubuntu answer to this question. – Camille Goudeseune May 30 '18 at 19:44
  • @Yatrix I am not wrong, Chrome and node use the same JS engine. Checked the `new Date()` and, indeed, the node REPL displays it in a different way, but `(new Date()).toString()` is the same in both. It won't affect the functionality of the script. – Ionică Bizău Jun 27 '19 at 05:57
  • @Yatrix That's what I mentioned above: when stringifying the dates, you will get exactly the same results. The way how they are displayed in the REPLs may be different (but the value is the same). – Ionică Bizău Jun 27 '19 at 11:28
136

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

microspino
  • 7,693
  • 3
  • 48
  • 49
  • Ubuntu doesn't support SpiderMonkey since 10.04. (Do you know why? http://askubuntu.com/q/180572/482285 ) – maciek Aug 10 '16 at 14:27
  • 1
    @maciek SpiderMonkey is still available for Ubuntu but under another name: http://packages.ubuntu.com/trusty/libmozjs-24-bin – microspino Aug 10 '16 at 16:31
  • 1
    @maciek ...so you can do: `sudo apt-get install libmozjs-24-bin` and the use `sudo ln -sf /usr/bin/js24 /usr/bin/js` to use it with *js* in your shell. – microspino Aug 10 '16 at 17:17
  • By the way `console.log` works fine with *node*, installed on Ubuntu with `sudo apt install nodejs`. Perfect for a `console.log('hey world, here I am')` kinda app, which you can run with `node hey.js`. – Nagev Feb 04 '20 at 17:24
  • On newer versions of the now called _MacOS_ the path to jsc is `/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc`. – cachius May 11 '22 at 19:32
129

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

Piper
  • 1,266
  • 3
  • 15
  • 26
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • 4
    FYI, difference between rhino and spidermonkey (both are Mozilla creations): http://stackoverflow.com/questions/3563909/rhino-vs-spidermonkey – Kelvin Jun 01 '12 at 16:48
  • 3
    On Debian / Ubuntu: 'apt-get install rhino' and binary is called js. – pevik Aug 29 '13 at 19:45
  • I don't know if you feel the same thing, but I'm finding rhyno pretty fragile and buggy... (running Ubuntu 14.04) – Ionică Bizău Oct 20 '14 at 08:56
  • 1
    This work for me, use print("msg") for debugging than using console.log() – Arefe Feb 02 '16 at 00:30
34

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.

Andrey Bodoev
  • 4,291
  • 2
  • 25
  • 19
  • This goes great in combination with https://github.com/remy/nodemon wich will watch any javascript file and rerun it on any change. So just run it with nodemon yourjavascript.js – Denis Oct 15 '16 at 15:16
  • 4
    Don't you mean `brew install node`? `install node` on its own won't really do anything. – numbermaniac Nov 08 '16 at 07:49
14

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.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
13

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

Steven
  • 1,071
  • 15
  • 15
Emjey
  • 2,038
  • 3
  • 18
  • 33
  • 2
    node is close to javascript but it is not exactly javascript, eg, javascript doesn't support file IO, there is require function by default either; it might not be a good way to test javascript file – Weijing Jay Lin Apr 17 '18 at 22:55
6

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
Mamrezo
  • 1,309
  • 16
  • 20
apflieger
  • 912
  • 10
  • 18
  • Thanks - exactly what I was looking for :-) – CharlesA Feb 09 '22 at 05:24
  • chmod u+x is better than +x because just add executable permits to the user and not to others. All this example is if you have Linux or Mac, not for Windows, worth to say. – Hender Apr 19 '22 at 17:59
5

Alternatively, if you're just looking to play around with Javascript a nice in browser option is Codecademy's Javascript Lab.

They also have a Python and Ruby IDE.

fscof
  • 1,593
  • 2
  • 17
  • 28
4

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.

4

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

Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
  • Is there one for Ubuntu, Google Chrome? – BLUC Dec 16 '11 at 10:51
  • You should have put that piece of vital information in the question. Sorry, I'm a Windows geek - I don't know much about Ubuntu. Besides, you will probably not have access to Google Chrome when running javascript from an Ubuntu shell prompt. EDIT: Andrzej Doyle's answer is the way to go for you. – Anders Marzi Tornblad Dec 16 '11 at 10:53
3

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

Keith Cascio
  • 2,868
  • 2
  • 11
  • 5
3

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

stevestar888
  • 113
  • 7
2

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

Alex
  • 12,078
  • 6
  • 64
  • 74
2

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.

Red Boy
  • 5,429
  • 3
  • 28
  • 41
0

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

ewalel
  • 1,932
  • 20
  • 25
-1

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.
redolent
  • 4,159
  • 5
  • 37
  • 47