简体   繁体   English

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

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

I have installed node-v0.10.13-x86.msi from nodejs.org 我已经从nodejs.org安装了node-v0.10.13-x86.msi
I have a file test.js having path (d:\\BNB\\test.js) 我有一个文件test.js有路径(d:\\ BNB \\ test.js)

test.js : test.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运行test.js但不能从node.js命令提示符下面参见截图

From start > run > cmd (working) 从开始>运行> cmd(工作)

在此输入图像描述

From Node.exe (not working) 从Node.exe(不工作) 在此输入图像描述

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. Node.exe是一个应用程序,可以用来从文件运行代码,或者自己运行代码,它的行为就像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,你将最终进入节点环境,并且那里的所有东西都不是cmd,而是JS。

If you type: node example.js that will call node environment and will execute file in that environment. 如果键入: node example.js将调用节点环境并将在该环境中执行文件。

Although, running node.exe is the same as typing node in cmd. 虽然,运行node.exe与在cmd中键入node相同。

in node REPL you could type 在节点REPL中,您可以键入

require('d:\\BNB\\test')

that shuold run your file.. shuold运行你的文件..

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中一样运行它。

We can however run tests on the command prompt once we have node.js installed. 但是,一旦安装了node.js,我们就可以在命令提示符下运行测试。

On the Windows command prompt type node ENTER 在Windows命令提示符下键入节点 ENTER

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!

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); 然后在系统变量中检查变量路径。安装Node.js后,它还应该有:(其他路径); C:\\Program Files\\nodejs\\ <-the link of Node.js directory C:\\ Program Files \\ nodejs \\ < - Node.js目录的链接

Click -OK- 点击-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'(node file.js)命令应该工作。这些步骤适用于我的问题。

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

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

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