Executing JavaScript without a browser?

JavascriptUnixCommand LineScriptingV8

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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-78-0 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-4.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.

Addendum

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 
#!/usr/bin/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!
$

Older Versions:
libmozjs-24-bin, libmozjs-52, libmozjs-60, libmozjs-91, libjavascriptcoregtk-3.0-bin

Solution 2 - Javascript

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

Solution 3 - Javascript

I have installed Node.js on an iMac and

node somefile.js

in bash will work.

Solution 4 - Javascript

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: https://stackoverflow.com/questions/7167690/what-is-the-progid-or-clsid-for-ie9s-javascript-engine-code-named-chakra/7168837#7168837 to learn how to invoke the higher-performance IE9 Javascript engine from cscript.exe.

Solution 5 - Javascript

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)

Solution 6 - Javascript

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

node-repl

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

Solution 7 - Javascript

I use Ubuntu 12.10 and js from commandline

It is available with my installation of java:

el@apollo:~/foo$ java -version
java version "1.6.0_27"
el@apollo:~/foo$ which js
/usr/bin/js

Some examples:

el@apollo:~/foo$ js
> 5
5

> console.log("hello");
hello
undefined

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

> var mybool = new Boolean();
undefined
> mybool
{}
> mybool == true
false
> mybool == false
true

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

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

Solution 8 - Javascript

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]

Solution 9 - Javascript

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

Solution 10 - Javascript

PhantomJS allows you to do this as well

http://phantomjs.org/

Solution 11 - Javascript

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!

Solution 12 - Javascript

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

Solution 13 - Javascript

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

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
QuestionDanielView Question on Stackoverflow
Solution 1 - JavascriptrbritoView Answer on Stackoverflow
Solution 2 - Javascriptjasonmp85View Answer on Stackoverflow
Solution 3 - JavascriptnonopolarityView Answer on Stackoverflow
Solution 4 - JavascriptCheesoView Answer on Stackoverflow
Solution 5 - JavascriptjbandiView Answer on Stackoverflow
Solution 6 - JavascriptthejefflarsonView Answer on Stackoverflow
Solution 7 - JavascriptEric LeschinskiView Answer on Stackoverflow
Solution 8 - JavascriptDaniel VassalloView Answer on Stackoverflow
Solution 9 - JavascriptneebzView Answer on Stackoverflow
Solution 10 - JavascriptSameer AlibhaiView Answer on Stackoverflow
Solution 11 - JavascriptPawan KumarView Answer on Stackoverflow
Solution 12 - JavascriptSean Patrick FloydView Answer on Stackoverflow
Solution 13 - JavascriptAndzView Answer on Stackoverflow