Executing JavaScript without a browser?
Asked Answered
D

14

140

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.

Dimity answered 31/5, 2010 at 4:48 Comment(5)
node.js is a javascript command line interpreter isn't it?Griffis
node.js is not an interpreter, it is an async framework built on the V8 javascript implementation from Google. Though I do believe V8's javascript intererpter is included in a node installCruce
If V8's CLI interpreter gets popular it could be a really good teaching tool. I think learning to program in JavaScript could be quite a bit of fun. Lua might honestly be the best choice (JS's "classes" confuse the hell out of everyone), but JS's ability to be thrown on the web would appeal to tinkerers. I may try out this V8 CLI myself.Toddler
@Toddler executing functions with a new keyword builds object instances. When you reference a property/method on an instance, if the object itself doesn't have it, the prototype property of the constructor function is checked. Use this for public methods and define vars local to the constructor function to effectively get private methods/props (these are technically closure ref'd vars). The end.Highchair
Node works to do this, but Deno is easier to install and allows you to import packages by urls instead of node modules. see deno.landManard
T
47

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

Toddler answered 31/5, 2010 at 5:12 Comment(4)
the Chrome/Google V8 interpreter link is exactly what I was looking for! Thank you!Dimity
NodeJS in 0.1.101 has a node-repl which gives you a CLI access to the environment. It's great for testing / learning JS.Pythagoreanism
With nodejs v0.10.25 on Ubuntu typing nodejs in terminal gives access to REPL.Vineland
Which of the above, if any, include the HTML DOM?Contextual
W
61

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

Weltpolitik answered 12/2, 2012 at 1:0 Comment(0)
T
47

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

Toddler answered 31/5, 2010 at 5:12 Comment(4)
the Chrome/Google V8 interpreter link is exactly what I was looking for! Thank you!Dimity
NodeJS in 0.1.101 has a node-repl which gives you a CLI access to the environment. It's great for testing / learning JS.Pythagoreanism
With nodejs v0.10.25 on Ubuntu typing nodejs in terminal gives access to REPL.Vineland
Which of the above, if any, include the HTML DOM?Contextual
D
23

I have installed Node.js on an iMac and

node somefile.js

in bash will work.

Driest answered 1/10, 2012 at 9:38 Comment(3)
OMG! @aw04 AbsolutelyOchone
for me it was nodejs somefile.js since I had installed the nodejs packageImam
this is the right answer in 2019. Node.JS is always the most ubiquitous and simplest way to go.Biogeography
P
15

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.

Pepsin answered 15/9, 2011 at 16:24 Comment(0)
C
9

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)

Cubature answered 23/8, 2013 at 22:2 Comment(0)
M
6

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.

Matthus answered 31/5, 2010 at 5:38 Comment(1)
I'm finding node-repl as well as using hash-bang both very good in getting quick programs working with NodeJS. It's fast and easy to use.Pythagoreanism
J
4

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.

Jelene answered 8/1, 2014 at 8:19 Comment(0)
O
3

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]
Ophthalmology answered 31/5, 2010 at 4:52 Comment(0)
I
3

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

Indifferentism answered 23/4, 2011 at 8:7 Comment(0)
Q
2

PhantomJS allows you to do this as well

http://phantomjs.org/

Quite answered 7/8, 2014 at 18:13 Comment(0)
T
2

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!

Tuyere answered 11/2, 2015 at 15:2 Comment(0)
R
1

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

Reactance answered 31/5, 2010 at 4:55 Comment(0)
I
1

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

Illegal answered 2/3, 2015 at 19:17 Comment(0)
T
0

Yes, you can use node.js environment for running js scripts.

Titrate answered 7/7, 2022 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.