如何运行JavaScript的Windows控制台 [英] How run a windows console for JavaScript

查看:172
本文介绍了如何运行JavaScript的Windows控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows 7上有一个控制台窗口(命令行),这将允许我像使用python控制台一样使用JavaScript。



更新:
从控制台(或通过它运行的脚本)访问文件很重要。

解决方案

您可以使用 Node.js 的REPL。为此,请按照以下步骤操作:


  1. 下载并安装Node.js

  2. 从开始菜单/开始屏幕或直接 node.exe调用Node.js 安装路径(例如 C:\Program Files\\\
    odejs\\\
    ode.exe
    )。

  3. li>

您可能希望将安装路径添加到PATH环境变量中以方便使用。



注意:要离开node.js,请按 Ctrl + C 两次。



< hr>

要访问本地文件,您需要文件系统模块。这是一个使用示例:

  var fs = require(fs); 
fs.readFile(
C:\\test.txt,
function(err,data)
{
if(!err)
console.log(data.toString());
}
);

这将输出文件的内容 C:\test.txt

:未处理的异常将导致node.js崩溃。




I would like to have a console window (a command line) on Windows 7 which will allow me to play with JavaScript just like a python console.

Update: It's important to have a file access from within the console (or script run through it).

解决方案

You can use Node.js's REPL. To do so follow this steps:

  1. Download and Install Node.js.
  2. Call Node.js from the Start Menu / Start Screen or directly node.exe installation path (e.g C:\Program Files\nodejs\node.exe).
  3. Enjoy!

You may want to add the installation path to your PATH enviroment variable for ease of use.

Note: to leave node.js press Ctrl + C twice.


To access the local files, you will need the File System module. This is an example of usage:

var fs = require("fs");
fs.readFile(
    "C:\\test.txt",
    function(err, data)
    {
        if (!err)
        console.log(data.toString());
    }
);

This will output the contents of the file C:\test.txt to the console.

Note: An unhandled exception will cause node.js to "crash".

这篇关于如何运行JavaScript的Windows控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆