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