简体   繁体   English

如何使用NodeJS在Windows中执行文件?

[英]How to execute a file in windows using NodeJS?

I installed Nodejs and I'm using it for the first time. 我安装了Nodejs,并且是第一次使用它。 The API is a bit too generic. 该API太通用了。 The first example does not explain how to specify a file path. 第一个示例没有说明如何指定文件路径。 I setup the testFile.js with the code: 我使用以下代码设置了testFile.js:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type':     
'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at     
http://127.0.0.1:8124/');

I verified node.js is correctly installed by typing "node <enter> 1 + 1" and got a response of "2". 我通过输入"node <enter> 1 + 1"验证了node.js的安装正确,并且得到的响应为“ 2”。 Then I tried: 然后我尝试了:

>node "C:\path\testNode.js"

This returns ... which is no different than passing an incorrect file 返回...与传递错误文件没有什么不同

>node "C:\\INCORRECTPATH\\testNode.js"

Then I tried dropping the parenthesis, still no work. 然后,我尝试删除括号,但仍然没有任何效果。 Also tried doing a change directory before running "node" command. 还尝试在运行“节点”命令之前执行更改目录。 Guess what! 你猜怎么了! Still no worky. 仍然没有工作。 The error ... is super useful, but I just dunno how to fix it..? 错误...超级有用,但我只是不知道如何解决..?

What's the syntax? 语法是什么?

UPDATE UPDATE

<AnticlimacticConclusion>
Restarting the computer corrected the problem. 重新启动计算机可以解决此问题。
</AnticlimacticConclusion>

So node.js did install . 所以node.js 确实安装了 And it even gave results (remember in the OP 1 + 1 returns 2). 而且它甚至给出了结果(在OP 1中记住1返回2)。 But, the PATH remained jacked up for whatever reason. 但是,无论出于何种原因,PATH仍然阻塞。 (Yes I tried a new instance of cmd). (是的,我尝试了一个新的cmd实例)。

(The rest of this post is old information for reference only) (本文的其余部分为旧信息,仅供参考)

What did not work 什么没有奏效

The commands did not function when I went to the command prompt via: 通过以下命令转到命令提示符时,命令不起作用:

start > run > cmd > node C:\\path\\testNode.js

It also did not function with a shortcut I had in my start menu to an admin mode version of cmd which links to: 我的开始菜单中的快捷方式也无法使用cmd的管理模式版本链接到:

%comspec% /k ""C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\Tools\\VsDevCmd.bat""

What worked 什么有效

I ran a search on my PC and found a program entitled "Node.js command prompt". 我在PC上进行了搜索,找到了一个名为“ Node.js命令提示符”的程序。 The shortcut on my machine pointed to: 我机器上的快捷方式指向:

C:\\Windows\\System32\\cmd.exe /k "C:\\Program Files\\nodejs\\nodevars.bat"

When I run node commands with this batch file pre-loaded then everything works out. 当我在预加载此批处理文件的情况下运行节点命令时,一切正常。 EG 例如

> node C:\\path\\testNode.js

returns Server running at http://127.0.0.1:8124/ . 返回Server running at http://127.0.0.1:8124/ A picture says 1000 words. 图片说1000字。 Note the first line. 注意第一行。 在此处输入图片说明

In other words, it appears this batch file is required. 换句话说,看来此批处理文件是必需的。

It looks like you're executing the node command inside a node repl. 看来您正在节点repl内执行node命令。

This would happen if you are working in the window that pops up when you click a "Node.js" icon in your start menu. 如果您在单击开始菜单中的“ Node.js”图标时弹出的窗口中工作,则会发生这种情况。

To execute it in the way you've tried, run it from a normal command prompt. 要以您尝试的方式执行它,请从常规命令提示符处运行它。 ie hit windows-r, type cmd, press return, or find Command Prompt in your start menu. 例如,打Windows-r,键入cmd,按回车键,或在开始菜单中找到命令提示符。

You can also run an external node script from inside the node repl by requiring it. 您也可以根据需要从节点repl内部运行外部节点脚本。

 require("C:\\path\\testNode.js")

This will only work the first time as it is cached for subsequent require calls. 这仅在第一次使用时有效,因为它被缓存用于后续的require调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM