windows - 如何在 Windows 上的 Node.js 中运行 hello.js 文件?

标签 windows node.js

我正在尝试在名为 hello.js 的单独文件中运行用 javascript 编写的 hello world 程序

当前运行的是windows版本的node.js。

代码在控制台窗口中完美运行,但是如何在windows环境中引用路径

C:\abc\zyx\hello.js

在 Unix 中我猜它显示 $ node hello.js

我是 Node.js 的新手,如果我做错了什么,请纠正我。

我试过了

> Node C:\abc\zyx\hello.js ----没用

> C:\abc\zyx\hello.js --没有用

更新1:

将 node.exe 添加到 hello.js 文件所在的文件夹中。
添加了指向文件夹 c:\abc\zyx\的路径点,我收到一条错误消息,提示

ReferenceError: hello 未定义

查看 hello.js 的内容

setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');

更新 2:

到目前为止,我已经尝试了所有这些版本,它们似乎都不起作用。可能是我做错了什么。

>node hello.js
>$ node hello.js
>node.exe hello.js
>node /hello.js
>node \hello.js
> \node \hello.js
> /node /hello.js
> C:\abc\xyz\node.exe C:\abc\xyz\hello.js
> C:\abc\xyz\node.exe C:/abc/xyz/hello.js
> hello.js
> /hello.js
> \hello.js
>node hello

引用我的文件结构

.
├── hello.js
├── node.exe
└── paths.txt

已解决: 不要运行 node.exe,而是尝试使用以下选项在命令提示符下运行,它可以工作。

c:\>node c:\abc\hello.js
Hello
World! (after 2 secs)

最佳答案

以下是我刚刚执行的运行“Hello World”示例的确切步骤,该示例位于 http://nodejs.org/。 .这是一个快速而肮脏的例子。对于永久安装,您希望将可执行文件存储在比根目录更合理的位置,并更新您的 PATH 以包含其位置。

  1. 在此处下载 Windows 可执行文件:http://nodejs.org/#download
  2. 将文件复制到 C:\
  3. 创建 C:\hello.js
  4. 粘贴以下内容:
    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(1337, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:1337/');
  1. 保存文件
  2. 开始 -> 运行... -> cmd
  3. c:
  4. C:>node hello.js

    Server running at http://127.0.0.1:1337/
    

就是这样。这是在 Windows XP 上完成的。

关于windows - 如何在 Windows 上的 Node.js 中运行 hello.js 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737824/

相关文章:

node.js - Nodejs内存使用情况

javascript - Node.js app.js 使 css 和 js 可用

windows - 运行 berks install 时出错

c# - 如何终止特定用户帐户下运行的特定进程

angularjs - API请求超时问题

Node.Js - 获取 Windows 用户名

javascript - 嵌套在 JSON 对象数组中的 JSON 对象数组

c++ - Windows 中的 WH_JOURNALRECORD Hook (C++) - 从未调用过回调。

windows - BAT 脚本发送命令给它启动的程序

c - Windows 的 rand_s 线程安全吗?