admin管理员组文章数量:1134244
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 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.
Share Improve this question edited Mar 4, 2016 at 12:36 nonopolarity 151k141 gold badges488 silver badges779 bronze badges asked May 31, 2010 at 4:48 DanielDaniel 7,1627 gold badges45 silver badges50 bronze badges 5- node.js is a javascript command line interpreter isn't it? – Nathan Commented May 31, 2010 at 5:00
- 5 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 install – user35288 Commented May 31, 2010 at 5:09
- 1 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. – jasonmp85 Commented May 31, 2010 at 5:20
- @jasonmp85 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. – Erik Reppen Commented Nov 13, 2012 at 17:30
- 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.land – José Mancharo Commented Aug 5, 2021 at 19:17
14 Answers
Reset to default 62Main 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 simplejs24
, which can be used also as an interactive interpreter. (The24
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 executablenodejs
and as an alternative (in the Debian-sense) to provide thejs
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
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).
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:
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.
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.
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 !
PhantomJS allows you to do this as well
http://phantomjs.org/
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
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
Yes, you can use node.js environment for running js scripts.
本文标签: unixExecuting JavaScript without a browserStack Overflow
版权声明:本文标题:unix - Executing JavaScript without a browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736775548a1952317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论