我正在尝试在名为 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
以包含其位置。
- 在此处下载 Windows 可执行文件:http://nodejs.org/#download
- 将文件复制到 C:\
- 创建 C:\hello.js
- 粘贴以下内容:
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/');
- 保存文件
- 开始 -> 运行... -> cmd
- c:
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/