Search code examples
javascriptunixcommand-linescriptingv8

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.


Solution

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