如何在 Windows 上的 Node.js 中运行 hello.js 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6737824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a hello.js file in Node.js on windows?
提问by Mitul
I am trying to run a hello world program written in javascript in a separate file named hello.js
我正在尝试在名为 hello.js 的单独文件中运行用 javascript 编写的 hello world 程序
Currently running windows version of node.js.
当前运行 Windows 版本的 node.js。
The code runs perfectly in console window but how do I reference the path in windows environment.
代码在控制台窗口中完美运行,但如何在 windows 环境中引用路径。
C:\abc\zyx\hello.js
in Unix I guess it is showing $ node hello.js
在 Unix 中,我猜它显示的是 $ node hello.js
I'm absolutely new to Node.js Please correct me if I am doing something wrong.
我对 Node.js 完全陌生,如果我做错了什么,请纠正我。
I tried
我试过
> node C:\abc\zyx\hello.js----didn't work
> node C:\abc\zyx\hello.js----没有用
> C:\abc\zyx\hello.js--didn't work
> C:\abc\zyx\hello.js--没用
UPDATE1:
更新1:
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
将 node.exe 添加到 hello.js 文件所在的文件夹中。
添加了指向文件夹 c:\abc\zyx\ 的路径点,我收到一条错误消息
ReferenceError: hello is not defined
ReferenceError: hello 未定义
see contents of hello.js
查看 hello.js 的内容
setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');
UPDATE 2:
更新 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
Refer to my file structure
参考我的文件结构
.
├── hello.js
├── node.exe
└── paths.txt
RESOLVED:Instead of running node.exe, try running in command prompt with the following option and it worked.
已解决:不要运行 node.exe,而是尝试使用以下选项在命令提示符下运行并且它起作用了。
c:\>node c:\abc\hello.js
Hello
World! (after 2 secs)
回答by Wayne
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 PATHto include its location.
以下是我刚刚在http://nodejs.org/上运行“Hello World”示例所采取的确切步骤。这是一个快速而肮脏的例子。对于永久安装,您希望将可执行文件存储在比根目录更合理的位置,并更新您PATH的位置以包含其位置。
- Download the Windows executable here: http://nodejs.org/#download
- Copy the file to C:\
- Create C:\hello.js
- Paste in the following content:
- 在此处下载 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/');
- Save the file
- Start -> Run... -> cmd
- c:
C:>node hello.js
Server running at http://127.0.0.1:1337/
- 保存文件
- 开始 -> 运行... -> cmd
- C:
C:> 节点 hello.js
Server running at http://127.0.0.1:1337/
That's it. This was done on Windows XP.
就是这样。这是在 Windows XP 上完成的。
回答by Venky
Install the MSI file:
Go to the installed directory C:\Program Files\nodejsfrom command prompt n
安装 MSI 文件:C:\Program Files\nodejs从命令提示符 n转到安装目录
C:\>cd C:\Program Files\nodejs enter..
C:\>cd C:\Program Files\nodejs enter..
node helloworld.js
node helloworld.js
output:
输出:
Hello World
Hello World
回答by kcbanner
You need to make sure that nodeis in your PATH. To set up your path, this out.
您需要确保它node在您的PATH. 要设置你的路径,这个 out。
Make sure that the directory that has node.exeis in your PATH. Then you should be able to
run node path_to_js_file.js.
确保具有的目录node.exe在您的PATH. 然后你应该能够运行node path_to_js_file.js.
For a good "Hello World" example, check out: http://howtonode.org/hello-node
对于一个好的“Hello World”示例,请查看:http: //howtonode.org/hello-node
回答by RAVI KIRAN
another simple way
另一种简单的方法
- download nodejs to your system
- open a notepad write js command "console.log('Hello World');"
- save the file as hello.js preferably same location as nodejs
- open command prompt navigate to the location where the nodejs is located
c:\program files\nodejs - and run the command from the location like
c:\program files\nodejs>node hello.js - in case the js file in another location give the path of file
c:\program files\nodejs>node path\hello.js
- 将 nodejs 下载到您的系统
- 打开记事本写js命令“console.log('Hello World');”
- 将文件保存为 hello.js 最好与 nodejs 相同的位置
- 打开命令提示符导航到nodejs所在的位置
c:\program files\nodejs - 并从以下位置运行命令
c:\program files\nodejs>node hello.js - 如果另一个位置的 js 文件给出了文件的路径
c:\program files\nodejs>node path\hello.js
回答by Grant
I installed node for windows. There is a node.js command prompt when I search for node.js in windows 7 start menu If you run this special command prompt, you can node anything in any location without setting up the path or copy node.exe everywhere.
我为 Windows 安装了节点。当我在 Windows 7 开始菜单中搜索 node.js 时,有一个 node.js 命令提示符如果你运行这个特殊的命令提示符,你可以在任何位置节点任何东西,而无需设置路径或到处复制 node.exe。
回答by Anatoly S
WinXp:
I have created a .batfile
WinXp:我已经创建了一个.bat文件
node c:\path\to\file\my_program.js
That just run my_program.batfrom Explorer or in cmd window
只是 my_program.bat从资源管理器或在 cmd 窗口中运行
回答by HUHO
Go to cmd and type: node "C:\Path\To\File\Sample.js"
转到 cmd 并键入:节点“C:\Path\To\File\Sample.js”
回答by Qasde
Windows/CMDdoes not know where the node file is located. You can manually type out:
Windows/CMD不知道节点文件在哪里。您可以手动输入:
path=%path%;"c:\Program Files\nodejs"
each time you open a new cmd.exe prompte
每次打开一个新的 cmd.exe 提示
OR (in Windows 10),
或(在 Windows 10 中),
- right click on
This PC->properties. - Click on
Advanced system settings->Environment Variables(bottom right). - Select
Pathand clickEdit. - Click new and enter
C:\Program Files\nodejs. - Reboot and you should be able to run node from any directory.
- 右键单击
This PC->properties。 - 单击
Advanced system settings->Environment Variables(右下角)。 - 选择
Path并单击Edit。 - 单击新建并输入
C:\Program Files\nodejs。 - 重新启动,您应该能够从任何目录运行节点。
回答by Bhavik patel
type node jscommand prompt in start screen. and use it.
OR
set PATHof node in environment variable.
node js在开始屏幕中键入命令提示符。并使用它。ORPATH环境变量中的节点集。
回答by Samuel Williams
All you have to do is right click the .js file on Windows and press "Open with Command Prompt" OROpen cmd, copy the path to the folder containing your script, and run the command "cd [paste text here]". Then do "node example.js"
您所要做的就是在 Windows 上右键单击 .js 文件,然后按“使用命令提示符 打开” 或打开 cmd,将路径复制到包含您的脚本的文件夹,然后运行命令“cd [在此处粘贴文本]”。然后做“节点example.js”

