简体   繁体   中英

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

I have installed node-v0.10.13-x86.msi from nodejs.org
I have a file test.js having path (d:\\BNB\\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

From start > run > cmd (working)

在此输入图像描述

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

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.

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

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

in node REPL you could type

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

that shuold run your file..

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.

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

On the Windows command prompt type node ENTER

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

console.log("Hello World!");

It replies with " 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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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