我正试图在一个名为hello.js的单独文件中运行一个使用javascript编写的hello.js程序。
目前正在运行node.js的windows版本。
代码在控制台窗口中运行得很好,但是如何在windows环境中引用路径。
C:\abc\zyx\hello.js在Unix中,我猜它显示的是$ node hello.js
我是Node.js的新手,如果我做错了什么,请纠正我。
我试过了
> node C:\abc\zyx\hello.js -没起作用
> C:\abc\zyx\hello.js --没起作用
UPDATE1:
将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)发布于 2011-07-18 20:04:34
下面是我在http://nodejs.org/上运行"Hello“示例所采取的具体步骤。这是一个快速而肮脏的例子。对于永久安装,您需要将可执行文件存储在比根目录更合理的位置,并更新PATH以包括其位置。
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/');就这样。这是在Windows上完成的。
发布于 2013-10-29 11:05:43
安装MSI文件:从命令提示符n转到已安装的目录C:\Program Files\nodejs
C:\>cd C:\Program Files\nodejs enter..
node helloworld.js
产出:
Hello World
发布于 2011-07-18 19:02:56
您需要确保node在您的PATH中。设置您的路径,这件事。
确保包含node.exe的目录位于PATH中。那么您应该能够运行node path_to_js_file.js了。
要获得一个好的"Hello“示例,请查看:http://howtonode.org/hello-node
https://stackoverflow.com/questions/6737824
复制相似问题