可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:\abc\zyx\hello.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:\abc\zyx\hello.js
----didn't work
> C:\abc\zyx\hello.js
--didn't work
UPDATE1:
Added node.exe to the folder where hello.js file is sitting.
Added path point to the folder c:\abc\zyx\ 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 > \node \hello.js > /node /hello.js > C:\abc\xyz\node.exe C:\abc\xyz\hello.js > C:\abc\xyz\node.exe C:/abc/xyz/hello.js > hello.js > /hello.js > \hello.js >node hello
Refer to my file structure
RESOLVED: Instead of running node.exe, try running in command prompt with the following option and it worked.
c:\>node c:\abc\hello.js Hello World! (after 2 secs)
回答1:
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.
- Download the Windows executable here: http://nodejs.org/#download
- Copy the file to C:\
- Create C:\hello.js
- 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\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');
- Save the file
- Start -> Run... -> cmd
- c:
C:>node hello.js
Server running at http://127.0.0.1:1337/
That's it. This was done on Windows XP.
回答2:
You need to make sure that node
is in your PATH
. To set up your path, this out.
Make sure that the directory that has node.exe
is in your PATH
. Then you should be able to run node path_to_js_file.js
.
For a good "Hello World" example, check out: http://howtonode.org/hello-node
回答3:
Install the MSI file: Go to the installed directory C:\Program Files\nodejs
from command prompt n
C:\>cd C:\Program Files\nodejs enter..
node helloworld.js
output:
Hello World
回答4:
another simple way
- download nodejs to your system
- open a notepad write js command "console.log('Hello World');"
- save the file as hello.js preferably same location as nodejs
- open command prompt navigate to the location where the nodejs is located
c:\program files\nodejs
- and run the command from the location like
c:\program files\nodejs>node hello.js
- in case the js file in another location give the path of file
c:\program files\nodejs>node path\hello.js
回答5:
I installed node for windows. There is a node.js command prompt when I search for node.js in windows 7 start menu If you run this special command prompt, you can node anything in any location without setting up the path or copy node.exe everywhere.
回答6:
WinXp: I have created a .bat
file
node c:\path\to\file\my_program.js
That just run my_program.bat
from Explorer or in cmd window
回答7:
type node js
command prompt in start screen. and use it. OR set PATH
of node in environment variable.
回答8:
Go to cmd and type: node "C:\Path\To\File\Sample.js"
回答9:
c:\> node.exe %CD%\hello.js
%CD% captures the current directory under DOS
回答10:
For all stuck on how to start!
https://github.com/sethvincent/javascripting
Copy here incase link dies:
- Open node.js command prompt
- Make directory called javascripting by typing "mkdir javascripting"
- Change directory into the javascripting folder by typing "cd javascripting"
- Create a file named introduction.js by typing "touch introduction.js" OR FOR WINDOWS: "NUL > introduction.js"
- Open the file and type some javascript e.g. "Console.log('hello');"
- Save the file and check it runs by typing "javascripting verify introduction.js"
回答11:
All you have to do is right click the .js file on Windows and press "Open with Command Prompt" OR Open cmd, copy the path to the folder containing your script, and run the command "cd [paste text here]". Then do "node example.js"