如何在Windows上的Node.js中运行hello.js文件? [英] How to run a hello.js file in Node.js on windows?

查看:122
本文介绍了如何在Windows上的Node.js中运行hello.js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run a hello world program written in javascript in a separate file named hello.js

当前正在运行Windows版本的node.js.

Currently running windows version of node.js.

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

The code runs perfectly in console window but how do I reference the path in windows environment.

C:\abc\zyx\hello.js

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

in Unix I guess it is showing $ node hello.js

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

I'm absolutely new to Node.js Please correct me if I am doing something wrong.

我尝试了

> node C:\abc\zyx\hello.js ----无效

> C:\abc\zyx\hello.js-无法正常工作

UPDATE1:

将node.exe添加到了hello.js文件所在的文件夹中. 添加了指向文件夹c:\ abc \ zyx \的路径,但出现错误

Added node.exe to the folder where hello.js file is sitting.
Added path point to the folder c:\abc\zyx\ and I get an error that says

ReferenceError:您好未定义

ReferenceError: hello is not defined

查看hello.js的内容

see contents of hello.js

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

更新2:

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

So far I have tried all these version and none of them seems to work. May be I am doing something completely wrong.

>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,尝试使用以下选项在命令提示符下运行,它会起作用.

RESOLVED: Instead of running node.exe, try running in command prompt with the following option and it worked.

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

推荐答案

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

Here are the exact steps I just took to run the "Hello World" example found at http://nodejs.org/. This is a quick and dirty example. For a permanent installation you'd want to store the executable in a more reasonable place than the root directory and update your PATH to include its location.

  1. 在此处下载Windows可执行文件: http://nodejs.org/#download
  2. 将文件复制到C:\
  3. 创建C:\ hello.js
  4. 粘贴以下内容:
  1. Download the Windows executable here: http://nodejs.org/#download
  2. Copy the file to C:\
  3. Create C:\hello.js
  4. Paste in the following content:

    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:>节点hello.js

  1. Save the file
  2. Start -> Run... -> cmd
  3. c:
  4. C:>node hello.js

Server running at http://127.0.0.1:1337/

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

That's it. This was done on Windows XP.

这篇关于如何在Windows上的Node.js中运行hello.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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