Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

How to run a hello.js file in Node.js on windows?

I am trying to run a hello world program written in javascript in a separate file named hello.js

Currently running windows version of node.js.

The code runs perfectly in console window but how do I reference the path in windows environment.

C:abczyxhello.js

in Unix I guess it is showing $ node hello.js

I'm absolutely new to Node.js Please correct me if I am doing something wrong.

I tried

> node C:abczyxhello.js ----didn't work

> C:abczyxhello.js --didn't work

UPDATE1:

Added node.exe to the folder where hello.js file is sitting.
Added path point to the folder c:abczyx and I get an error that says

ReferenceError: hello is not defined

see contents of hello.js

setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');

UPDATE 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
> 
ode hello.js
> /node /hello.js
> C:abcxyz
ode.exe C:abcxyzhello.js
> C:abcxyz
ode.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.

c:>node c:abchello.js
Hello
World! (after 2 secs)
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 PATH to include its location.

  1. Download the Windows executable here: http://nodejs.org/#download
  2. Copy the file to C:
  3. Create C:hello.js
  4. Paste in the following content:
    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World
');
    }).listen(1337, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:1337/');
  1. Save the file
  2. Start -> Run... -> cmd
  3. c:
  4. C:>node hello.js

    Server running at http://127.0.0.1:1337/
    

That's it. This was done on Windows XP.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...