在 Windows 7 中从 node.js 命令提示符运行 Hello world

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17654610/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:02:14  来源:igfitidea点击:

Run Hello world from node.js command prompt in windows 7

windowsnode.jscommand-prompt

提问by Satinder singh

I have installed node-v0.10.13-x86.msi from nodejs.org
I have a file test.js having path (d:\BNB\test.js)

我已经从nodejs.org安装了 node-v0.10.13-x86.msi
我有一个文件 test.js 有路径 (d:\BNB\test.js)

test.js:

测试.js

console.log("Hello world!");

I am able to run test.js from my cmd but not from node.js command prompt refer below screenshot

我可以从我的 cmd 但不能从 node.js 命令提示符运行 test.js 请参考下面的截图

From start > run > cmd (working)

从开始 > 运行 > cmd(工作)

enter image description here

在此处输入图片说明

From Node.exe (not working) enter image description here

从 Node.exe(不工作) 在此处输入图片说明

采纳答案by moka

Node.exe is application that can be used to run code from file, or to be ran by it self that way it will behave like live-terminal.
Once you run node.exe like you did in second example, you will end up in node environment and everything from there is not cmd at all, but JS.

Node.exe 是可用于从文件运行代码的应用程序,或者由它自己运行,这样它的行为就像实时终端一样。
一旦像在第二个示例中那样运行 node.exe,您将最终进入 node 环境,并且那里的所有内容都不是 cmd,而是 JS。

If you type: node example.jsthat will call node environment and will execute file in that environment.

如果您键入:node example.js这将调用节点环境并将在该环境中执行文件。

Although, running node.exe is the same as typing nodein cmd.

虽然,运行 node.exe 与输入nodecmd相同。

回答by salek

in node REPL you could type

在节点 REPL 中,您可以输入

require('d:\BNB\test')

that shuold run your file..

那应该运行你的文件..

回答by I'm Poor

You are trying to execute the program from the node prompt. You don't do that. You just run the node terminal. It sets up a bunch of variables for you. Just run it like you do in the ordinary windows shell.

您正在尝试从节点提示执行程序。你不要那样做。您只需运行节点终端。它为您设置了一堆变量。就像在普通的 Windows shell 中一样运行它。

回答by tony

We can however run tests on the command prompt once we have node.js installed.

然而,一旦我们安装了 node.js,我们就可以在命令提示符下运行测试。

On the Windows command prompt type nodeENTER

在 Windows 命令提示符下键入nodeENTER

Then we can run our JavaScript code tests just like we do on any other Console:

然后我们可以像在任何其他控制台上一样运行我们的 JavaScript 代码测试:

console.log("Hello World!");

console.log("Hello World!");

It replies with "Hello World!"

它回复“ Hello World!

回答by iakwvina

To show the correct directory in Node.js you need to create the right path in your System: System > Advanced System Settings > Advanced > Enviroment Variables

要在 Node.js 中显示正确的目录,您需要在系统中创建正确的路径:系统 > 高级系统设置 > 高级 > 环境变量

Then in the System variables check for Variable Path.After you have installed the Node.js it should also have :(other paths); C:\Program Files\nodejs\ <-the link of Node.js directory

然后在系统变量中检查变量路径。安装 Node.js 后,它也应该有 :(other paths); C:\Program Files\nodejs\ <-Node.js 目录的链接

Click -OK-

单击-确定-

Now if you open Command Prompt, and you go to the directory of your .js file, the 'node' (node file.js) command should work.These steps worked to my problem.

现在,如果您打开命令提示符,并转到 .js 文件的目录,“节点”(node file.js)命令应该可以工作。这些步骤可以解决我的问题。

回答by naveen kumar

i believe u need to start node server from the location where your file is.

我相信你需要从文件所在的位置启动节点服务器。