Executing JavaScript without a browser?

I am looking into Javascript programming without a browser. I want to run scripts from the Linux or Mac OS X command line, much like we run any other scripting language (ruby, php, perl, python...)

$ javascript my_javascript_code.js

I looked into spider monkey (Mozilla) and v8 (Google), but both of these appear to be embedded.

Is anyone using Javascript as a scripting language to be executed from the command line?

If anyone is curious why I am looking into this, I've been poking around node.js. The performance of node.js makes me wonder if javascript may be a viable scripting language for processing large data.



I found this related question on the topic, but if you want direct links, here they are:

  • You can install Rhino as others have pointed out. This post shows an easy way to get it up and running and how to alias a command to invoke it easily
  • If you're on a Mac, you can use JavaScriptCore, which invokes WebKit's JavaScript engine. Here's a post on it
  • You can use Chome/Google's V8 interpreter as well. Here are instructions
  • The JavaScript as OSA is interesting because it lets you (AFAIK) interact with scriptable OS X apps as though you were in AppleScript (without the terrible syntax)

I'm surprised node.js doesn't come with a shell, but I guess it's really more like an epoll/selector-based callback/event-oriented webserver, so perhaps it doesn't need the full JS feature set, but I'm not too familiar with its inner workings.

Since you seem interested in node.js and since it's based on V8, it might be best to follow those instructions on getting a V8 environment set up so you can have a consistent basis for your JavaScript programming (I should hope JSC and V8 are mostly the same, but I'm not sure).


Main Answer

Yes, to answer your question, it is possible to use JavaScript as a "regular" scripting language from the command line, without a browser. Since others have not mentioned it yet, I see that it is worth mentioning:

On Debian-based systems (and this includes Ubuntu, Linux Mint, and aptosid/sidux, at least), besides the options of installing Rhino and others already mentioned, you have have other options:

  • Install the libmozjs-24-bin package, which will provide you with Mozilla's Spidermonkey engine on the command line as a simple js24, which can be used also as an interactive interpreter. (The 24 in the name means that it corresponds to version 24 of Firefox).

  • Install the libv8-dev package, which will provide you Google's V8 engine. It has, as one of its examples, the file /usr/share/doc/libv8-dev/examples/shell.cc.gz which you can uncompress and compile very simply (e.g., g++ -Os shell.cc -o shell -lv8).

  • Install the package nodejs and it will be available both as the executable nodejs and as an alternative (in the Debian-sense) to provide the js executable. JIT compilation is provided as a courtesy of V8.

  • Install the package libjavascriptcoregtk-3.0-bin and use WebKit's JavaScriptCore interpreter (jsc) as a regular interpreter from the command-line. And this is without needing to have access to a Mac. On many platforms (e.g., x86 and x86_64), this interpreter will come with a JIT compiler.

So, with almost no compilation you will have three of the heavy-weight JavaScript engines at your disposal.


Once you have things installed, you can simply create files with the #!/usr/bin/js shebang line and things will just work:

$ cat foo.js 

console.log("Hello, world!");
$ ls -lAF /usr/bin/js /etc/alternatives/js /usr/bin/nodejs
lrwxrwxrwx 1 root root      15 Jul 16 04:26 /etc/alternatives/js -> /usr/bin/nodejs*
lrwxrwxrwx 1 root root      20 Jul 16 04:26 /usr/bin/js -> /etc/alternatives/js*
-rwxr-xr-x 1 root root 1422004 Apr 28 20:31 /usr/bin/nodejs*
$ chmod a+x foo.js 
$ ./foo.js 
Hello, world!
$ js ./foo.js
Hello, world!

I have installed Node.js on an iMac and

node somefile.js

in bash will work.


I know you asked about Linux and Mac; I am going to provide the answer for Windows, in case other people who are interested in Windows find your question .

Windows includes a Javascript engine that can be used from the command line.

All versions of Windows, since Windows 98, have included something called "The Windows Script Host". It's a windows-standard way to support script "engines". Since the first release, WSH supports JScript, Microsoft's version of Javascript. Among other things, this means that, from a windows command line, you can just invoke the name of any *.js file, and it will run in the JScript engine. (via either wscript.exe or cscript.exe)

You can see this question: What is the ProgId or CLSID for IE9's Javascript engine (code-named "Chakra") to learn how to invoke the higher-performance IE9 Javascript engine from cscript.exe.


Since nobody mentioned it: Since Java 1.6 The Java JDK also comes bundled with a JavaScript commandline and REPL.

It is based on Rhino: https://developer.mozilla.org/en/docs/Rhino

In Java 1.6 and 1.7 the command is called jrunscript (jrunscript.exe on Windows) and can be found in the bin folder of the JDK.

Starting from Java 1.8 there is bundled a new JavaScript implementation (Nashorn: https://blogs.oracle.com/nashorn/)

So in Java 1.8 the command is called jjs (jjs.exe on Windows)


FWIW, node.js comes with a shell, try typing in:


once you've installed node.js to see it in action. It's pretty standard to install rlwrap to get it to work nicely.


I use Ubuntu 12.10 and js from commandline

It is available with my installation of java:

[email protected]:~/foo$ java -version
java version "1.6.0_27"
[email protected]:~/foo$ which js

Some examples:

[email protected]:~/foo$ js
> 5

> console.log("hello");

> var f = function(){ console.log("derp"); };
> f();

> var mybool = new Boolean();
> mybool
> mybool == true
> mybool == false

> var myobj = {};
> myobj.skipper = "on my mark, engage!"
'on my mark, engage!'
> myobj.skipper.split(" ");
[ 'on',
  'engage!' ]

The sky is the limit, then keep right on going.


PhantomJS allows you to do this as well



I found this really nifty open source ECMAScript compliant JS Engine completely written in C called duktape

Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint.

Good luck!


Well there is JavaScript as OSA, an extension that provides JavaScript as an alternative to appleScript. I've been using that about 10 years ago, don't know if it's still working with current OS versions


You may want to check out Rhino.

The Rhino Shell provides a way to run JavaScript scripts in batch mode:

java org.mozilla.javascript.tools.shell.Main my_javascript_code.js [args]

I know this is old but you should also try Zombie.js. A headless browser which is insanely fast and ideal for testing !


JSDB, available for Linux, Windows, and Mac should fit the bill pretty well. It uses Mozilla's Spidermonkey Javascript engine and seems to be less of a hassle to install compared to node.js (at least last time I tried node.js a couple of years ago).

I found JSDB from this interesting list of Javascript shells: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.