无需浏览器即可执行 JavaScript?
我正在研究没有浏览器的 Javascript 编程。我想从 Linux 或 Mac OS X 命令行运行脚本,就像我们运行任何其他脚本语言(ruby、php、perl、python...)一样。
$ javascript my_javascript_code.js
我研究了蜘蛛猴(Mozilla)和 v8(Google),但是这两者似乎都是嵌入的。
有人使用 Javascript 作为从命令行执行的脚本语言吗?
如果有人好奇我为什么要研究这个,我一直在研究 node.js。 Node.js 的性能让我想知道 javascript 是否可能是处理大数据的可行脚本语言。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
主要答案
是的,为了回答您的问题,可以从命令行使用 JavaScript 作为“常规”脚本语言,而无需浏览器。由于其他人还没有提到它,我认为值得一提:
在基于 Debian 的系统上(至少包括 Ubuntu、Linux Mint 和 aptosid/sidux),除了安装 Rhino 和其他已经提到的选项之外,您还有其他选择:
安装
libmozjs-78-0
< /a> 包,它将在命令行上为您提供 Mozilla 的 Spidermonkey 引擎作为简单的 js24,它也可以用作交互式解释器。 (名称中的24
表示它对应的是Firefox的24版本)。安装
libv8-dev
软件包,它将提供你是谷歌的V8引擎。作为其示例之一,它具有文件/usr/share/doc/libv8-dev/examples/shell.cc.gz
,您可以非常简单地解压缩和编译该文件(例如g++ -Os shell.cc -o shell -lv8
)。安装包
nodejs
,它将作为可执行nodejs
并作为替代< /a> (在 Debian 意义上)提供js
可执行文件。 JIT 编译是由 V8 提供的。安装软件包
libjavascriptcoregtk-4.0-bin
并使用 WebKit 的 JavaScriptCore 解释器 (jsc
) 作为命令行中的常规解释器。而且这不需要访问 Mac。在许多平台(例如 x86 和 x86_64)上,该解释器将附带 JIT 编译器。因此,几乎不需要编译,您就可以使用三个重量级 JavaScript 引擎。
附录
一旦安装完毕,您只需使用
#!/usr/bin/js
shebang 行创建文件,一切就会正常工作:旧版本:
libmozjs-24-bin,libmozjs-52, libmozjs- 60,libmozjs-91,libjavascriptcoregtk-3.0-bin
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 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:Older Versions:
libmozjs-24-bin, libmozjs-52, libmozjs-60, libmozjs-91, libjavascriptcoregtk-3.0-bin
我发现了这个关于该主题的相关问题,但如果您想要直接链接,这里有是:
但我很惊讶node.js没有附带shell,但我想它实际上更像是一个基于epoll/选择器的回调/面向事件的网络服务器,所以也许它没有不需要完整的 JS 功能集,但我不太熟悉其内部工作原理。
由于您似乎对 Node.js 感兴趣并且它基于 V8,因此最好遵循有关设置 V8 环境的说明,以便您可以为 JavaScript 编程拥有一致的基础(我希望 JSC 和 V8 主要是一样,但我不确定)。
I found this related question on the topic, but if you want direct links, here they are:
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).
我已经在 iMac 上安装了 Node.js,并且
在 bash 中可以工作。
I have installed Node.js on an iMac and
in bash will work.
我知道你问过 Linux 和 Mac;我将提供Windows的答案,以防其他对Windows感兴趣的人发现你的问题。
Windows 包含一个可以从命令行使用的 Javascript 引擎。
自 Windows 98 以来,所有版本的 Windows 都包含名为“Windows 脚本宿主"。这是支持脚本“引擎”的 Windows 标准方式。自第一个版本以来,WSH 支持 JScript,即 Microsoft 的 Javascript 版本。除此之外,这意味着,您可以从 Windows 命令行调用任何 *.js 文件的名称,并且它将在 JScript 引擎中运行。 (通过 wscript.exe 或 cscript.exe)
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)
因为没有人提到它:从 Java 1.6 开始,Java JDK 还捆绑了 JavaScript 命令行和 REPL。
它基于 Rhino:https://developer.mozilla.org/en/docs/Rhino
在 Java 1.6 和 1.7 中,该命令称为
jrunscript
(在 Windows 上为jrunscript.exe
),可以在 JDK 的 bin 文件夹中找到。从 Java 1.8 开始,捆绑了一个新的 JavaScript 实现(Nashorn:https://blogs.oracle.com/nashorn/ )
因此在 Java 1.8 中该命令称为
jjs
(在 Windows 上为jjs.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 带有一个 shell,请尝试输入:
一旦安装了 node.js 即可查看它的运行情况。安装 rlwrap 以使其正常工作是非常标准的。
FWIW, node.js comes with a shell, try typing in:
once you've installed node.js to see it in action. It's pretty standard to install rlwrap to get it to work nicely.
我使用 Ubuntu 12.10 和命令行中的 js
它可以通过我安装的 java 来使用:
一些示例:
天空是极限,然后继续前进。
I use Ubuntu 12.10 and js from commandline
It is available with my installation of java:
Some examples:
The sky is the limit, then keep right on going.
您可能想查看 Rhino。
Rhino Shell 提供了一种以批处理模式运行 JavaScript 脚本的方法:
You may want to check out Rhino.
The Rhino Shell provides a way to run JavaScript scripts in batch mode:
我知道这已经过时了,但你也应该尝试 Zombie.js。一个无头浏览器,速度非常快,非常适合测试!
I know this is old but you should also try Zombie.js. A headless browser which is insanely fast and ideal for testing !
PhantomJS 也允许您执行此操作
http://phantomjs.org/
PhantomJS allows you to do this as well
http://phantomjs.org/
我发现这个非常漂亮的开源 ECMAScript 兼容 JS 引擎完全用 C 编写,名为 duktape
祝你好运!
I found this really nifty open source ECMAScript compliant JS Engine completely written in C called duktape
Good luck!
嗯,有 JavaScript as OSA,一个提供 JavaScript 作为 OSA 替代方案的扩展苹果脚本。我大约 10 年前就已经使用过它,不知道它是否仍然适用于当前的操作系统版本
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,适用于 Linux、Windows 和 Mac 应该非常适合这个要求。它使用 Mozilla 的 Spidermonkey Javascript 引擎,与 Node.js 相比,安装似乎不那么麻烦(至少几年前我上次尝试 Node.js 时是这样)。
我从这个有趣的 Javascript shell 列表中找到了 JSDB:https://developer。 mozilla.org/en-US/docs/Web/JavaScript/Shells
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
是的,您可以使用node.js环境来运行js脚本。
Yes, you can use node.js environment for running js scripts.