是否可以从命令行运行 JavaScript 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12659778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:57:51  来源:igfitidea点击:

Is it possible to run JavaScript files from the command line?

javascriptshellconsole

提问by Beau Smith

I have several JS files so instead of copy and paste every one in the console window (Firefox and Chromium), I would want to call them from the shell if it is possible.

我有几个 JS 文件,所以如果可能的话,我不想在控制台窗口(Firefox 和 Chromium)中复制和粘贴每个文件,而是想从 shell 调用它们。

Every JS file has test functions that show if they are correct using console.log.

每个 JS 文件都有测试函数,使用 console.log 显示它们是否正确。

采纳答案by Matt

If your tests need access to a DOM there is always PhantomJS- a headless (Webkit) browser.

如果您的测试需要访问 DOM,那么总是有PhantomJS- 一个无头 (Webkit) 浏览器。

回答by Beau Smith

Expanding upon the solution to use Node.js…

扩展使用 Node.js 的解决方案......

Here are some examples and screenshots from a page on Command Line JavaScript.

以下是来自命令行 JavaScript页面的一些示例和屏幕截图。

The Node REPL (Shell)

节点 REPL(外壳)

If you enter nodeon the command line with no arguments, you'll be in the Read-Eval-Print-Loop, or REPL for short, otherwise known as a shell. Here you can interactively enter JavaScript expressions and have them immediately evaluated.

如果您node在不带参数的命令行上输入,您将进入 Read-Eval-Print-Loop,或简称 REPL,也称为 shell。在这里,您可以交互式地输入 JavaScript 表达式并立即对其进行评估。

Node REPL (Shell)

节点 REPL(外壳)

Evaluate a JavaScript file from the command line

从命令行评估 JavaScript 文件

Create a file with the following content:

创建一个包含以下内容的文件:

console.log('Hello, world');

From the command line, use nodeto evaluate the file:

在命令行中,使用node来评估文件:

Evaluate a JavaScript file

评估 JavaScript 文件

回答by Rohit Luthra

I am not saying that it is the best solution but it is one of the available options. I just want to spread awareness and one reason this is how Java runs javascript because it has an embedded JavaScript runtime already for a long time. First there was Rhino, and now, Java SE 8 shipped with a new engine called Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance through invokedynamic-bound call sites. It can be used to run JavaScript programs from the command line. To do so, builds of Oracle's JDK or OpenJDK include a command-line tool called jjs. It can be found in the bin/ folder of a JDK installation along with the well-known java, javac, or jar tools.

我并不是说这是最好的解决方案,但它是可用的选项之一。我只是想传播意识,其中一个原因是 Java 运行 javascript 的方式,因为它已经有一个嵌入式 JavaScript 运行时已经很长时间了。首先是 Rhino,现在,Java SE 8 附带了一个名为Nashorn的新引擎,它基于 JSR 292 和调用动态。它通过调用动态绑定的调用站点更好地符合 ECMA 规范化 JavaScript 规范和更好的运行时性能。它可用于从命令行运行 JavaScript 程序。为此,Oracle 的 JDK 或 OpenJDK 的构建包括一个名为jjs的命令行工具。它可以在 JDK 安装的 bin/ 文件夹中与众所周知的 java、javac 或 jar 工具一起找到。

The jjs tool accepts a list of JavaScript source code files as arguments. Consider the following hello.js file:

jjs 工具接受 JavaScript 源代码文件列表作为参数。考虑以下 hello.js 文件:

var hello = function() {
  print("Hello Nashorn!");
};

hello(); 

Evaluating it is as simple as this:

评估它就像这样简单:

$ jjs hello.js
Hello Nashorn!
$

For more detail you can refer official documentation http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html

有关更多详细信息,您可以参考官方文档http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html

回答by craigmj

You can do this using node.js. You can run each file separately, but of course I'm assuming there's no dependency between files.

您可以使用node.js执行此操作。您可以单独运行每个文件,但当然我假设文件之间没有依赖关系。

This windows command line javascriptdiscusses using Windows Scripting Host if you're on Windows and don't want to install Node. But Node is probably the better bet for standardized js (it uses the v8 Javascript engine).

如果您使用的是 Windows 并且不想安装 Node.js,则此Windows 命令行 javascript讨论了如何使用 Windows Scripting Host。但是 Node 可能是标准化 js 更好的选择(它使用 v8 Javascript 引擎)。

回答by Kiran Kumar Chirravuri

Yeah, It can be possible with node. Just create a js file, write some code, save that and go to the directory where you have saved your file, then enter node <your file_name>. You are done. Note: You must have nodeinstalled on your system.

是的,它可以与node. 只需创建一个 js 文件,编写一些代码,保存并转到您保存文件的目录,然后输入node <your file_name>. 你完成了。注意:node的系统上必须安装了.

回答by primehunter

For those in hurry and on Windows, just try the following commandline in the command prompt:

对于那些赶时间和在 Windows 上的人,只需在命令提示符下尝试以下命令行:

cscript /E:jscript myJavaScriptFile.js