341

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

.
├── 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:\abc\hello.js
Hello
World! (after 2 secs)
Jonta
  • 386
  • 5
  • 25
Mitul
  • 9,154
  • 3
  • 38
  • 59
  • 1
    Could you provide a little more information? There is no official node for Windows as far as I know, seeing as the announcement that the port was starting was only made [a few weeks ago](http://blog.nodejs.org/2011/06/23/porting-node-to-windows-with-microsoft%E2%80%99s-help/). Are you using node.js under Cygwin, or is there some other node windows fork out there (if there is, I haven't heard of it). EDIT: I didn't realize that there was a preview build available. My apologies. – Doug Stephen Jul 18 '11 at 19:12
  • 5
    http://nodejs.org/dist/v0.5.1/node.exe – kcbanner Jul 18 '11 at 19:16
  • 7
    Try `node hello.js` instead of `node hello` ;) – Raynos Jul 18 '11 at 19:23
  • 1
    i dont know what c:> is but from the command prompt, as you said, "node c:\users\me\desktop\helloworld.js" is what worked for me. – user1873073 Dec 31 '12 at 21:24
  • 2
    I had the same problems as you, it's funny, because even today 2016, I found dozens of tutorials trying to teach nodejs but none of them teach step by step how to run the basic command besides the '$ node filename.js'. I can't understand how everybody assume that we need to use the root folder for this. Who uses the disk root to host files? – Diego Mendes Jan 16 '16 at 01:27
  • @DiegoMendes I think that $ is linux not windows – Jason K. Aug 09 '17 at 21:08

17 Answers17

393

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\n');
    }).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.

Wayne
  • 56,476
  • 13
  • 125
  • 118
  • 11
    Works like a Charm!!! Thanks a lot. The trick is to open cmd instead of node.exe command prompt. – Mitul Jul 18 '11 at 20:11
  • 1
    @Mitul - Right, this is the detail that was missing from your description -- i.e. how you were starting node -- that was preventing others from helping you. Starting node with no arguments drops you into the REPL. This is an environment for interactively executing javascript. It's not the place to kick-off the execution of a file. More here: http://nodejs.org/docs/v0.3.1/api/repl.html – Wayne Jul 18 '11 at 20:18
  • 3
    [Here is a screencast which shows how to install the latest node.js and npm versions on windows.](http://www.youtube.com/watch?v=IbgNedrI24o) – balupton Nov 14 '11 at 14:14
  • Tried many options here and there but this one was simple and exactly one that is needed For node on Windows. – RohitWagh Apr 30 '12 at 18:41
  • 10
    This is awful, _DO NOT_ copy the node executable into the root of your hard drive. This is what paths are meant for, see @kcbanner's answer. Also, the Window's install of Node comes with a Node.js command prompt that has the paths properly set already if you don't want to modify your system path. – joshperry Oct 21 '13 at 17:00
  • 2
    @joshperry - I think you're slightly missing the point. The question has been heavily edited from the original, but the main problem the OP was having is that they were trying to execute a file from the REPL. Nothing about my answer is intended as a long-term installation solution, nor is that even implied. The instructions are so simple precisely to illustrate the difference between executing a `.js` file and opening the REPL. – Wayne Oct 22 '13 at 16:47
  • 1
    Oh, also, dropping the `node.exe` into the root was an entirely arbitrary choice. It wasn't designed to circumvent setting up a proper PATH. It was just an entirely arbitrary place to save the file. The only reason I named a *specific* place is so that the steps would be explicit. Of course you should setup your path, but that wouldn't have helped the OP. See their comment above. They were literally *running* the `exe` first (probably by double-clicking it) and then behaving as though they were on the command line. – Wayne Oct 22 '13 at 16:54
  • 1
    Yes, makes sense. Unfortunately, and for posterity, this is the top result on Google for running a '.js' file using node on Windows. – joshperry Oct 22 '13 at 17:57
  • @joshperry - Hah, that explains the drive-by upvotes it's been getting. Let me see if I can make the answer a little more clear. – Wayne Oct 22 '13 at 19:29
  • 1
    This helped me get running, using my own command prompt window. I can't quite understand why the examples on the web and the node js website don't make it more clear how to run their simple example. It's not as simple as installing node.js and running it and specifying a file. I had to ask - where am I executing the file from, etc... – theJerm Dec 03 '13 at 07:43
  • DO NOT USE node.js cmd but windows cmd THEY DO LOOK SIMILAR. Win 7 32 bit in my case node console was added to my start menu, but it is not the same as the windows console. – Petar Drianov Jul 26 '14 at 11:56
  • If I want to keep my file or project folder to another drive/or folder. then? – Md Wahid Jan 19 '20 at 15:07
40

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

oz123
  • 23,317
  • 25
  • 106
  • 169
Venky
  • 401
  • 4
  • 2
  • 1
    [Setting Node in Environment Variable will easy things](http://stackoverflow.com/a/9737091/452708) – Abhijeet Jul 25 '16 at 09:04
32

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

Priyantha
  • 3,687
  • 4
  • 21
  • 40
kcbanner
  • 3,800
  • 1
  • 22
  • 18
  • I added node.exe to the folder where hello.js file is sitting. Also added path point to the folder c:\abc\zyx\ and I get error – Mitul Jul 18 '11 at 19:16
  • What is the error? It is hard to provide any help without knowing what the actual error is. – kcbanner Jul 18 '11 at 19:18
  • 1
    Sorry I forgot mention earlier. Please refer to my update in the question. Thanks for helping. – Mitul Jul 18 '11 at 19:19
  • Oh, I see. That looks like a problem inside your hello.js file. Please post the contents of that file – kcbanner Jul 18 '11 at 19:20
  • 1
    I've updated my answer with a good "Hello World" example for node. – kcbanner Jul 18 '11 at 19:22
  • Are you sure you used real ASCII ' characters for quotes? Are you sure you only have 1 hello.js on your hard drive? – nponeccop Jul 18 '11 at 19:37
  • I used textpad to write this code and there is only one version of this file. – Mitul Jul 18 '11 at 19:57
  • This is good advice, but if you look at the OP's comment on my answer, you'll see that their issue was much more basic. They were *running* `node.exe` and then running commands in the REPL as though they were on the command line. Still, +1 for good advice. – Wayne Oct 22 '13 at 16:56
  • @kcbanner I've been pulling my hair trying to set up Node among others, also mongodb. I understand global variables, but could you explain why on my home computer none of those gets added to PATH automatically? They did on my work computer where a colleague showed me how do to it >:( Its VERY frustrating, cant find a good answer – Cammy Feb 18 '14 at 21:01
8

another simple way

  1. download nodejs to your system
  2. open a notepad write js command "console.log('Hello World');"
  3. save the file as hello.js preferably same location as nodejs
  4. open command prompt navigate to the location where the nodejs is located
    c:\program files\nodejs
  5. and run the command from the location like c:\program files\nodejs>node hello.js
  6. in case the js file in another location give the path of file c:\program files\nodejs>node path\hello.js
Horizon_Net
  • 5,770
  • 4
  • 28
  • 34
RAVI KIRAN
  • 89
  • 1
  • 1
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.

Grant
  • 51
  • 1
  • 1
3

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

Marcelo
  • 4,371
  • 7
  • 28
  • 45
Anatoly S
  • 31
  • 2
3

Go to cmd and type: node "C:\Path\To\File\Sample.js"

HUHO
  • 51
  • 1
  • 3
3

Windows/CMD does not know where the node file is located. You can manually type out:

path=%path%;"c:\Program Files\nodejs"

each time you open a new cmd.exe prompte

OR (in Windows 10),

  1. right click on This PC -> properties.
  2. Click on Advanced system settings -> Environment Variables (bottom right).
  3. Select Path and click Edit.
  4. Click new and enter C:\Program Files\nodejs.
  5. Reboot and you should be able to run node from any directory.
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Qasde
  • 31
  • 1
2

The problem was that you opened the Node.js repl while everyone automatically assumed you were in the command prompt. For what it's worth you can run a javascript file from the repl with the .load command. For example:

.load c:/users/username/documents/script.js

The same command can also be used in the command prompt if you first start node inside the command prompt by entering node with no arguments (assuming node is in PATH).

I find it fascinating that 1)everyone assumed you were in the command prompt rather than repl, 2)no one seems to know about .load, and 3)this has 273 upvotes, proving that a lot of other node.js beginners are similarly confused.

Matthew
  • 3,560
  • 1
  • 19
  • 44
1

type node js command prompt in start screen. and use it. OR set PATH of node in environment variable.

fehrlich
  • 2,263
  • 2
  • 24
  • 41
Bhavik patel
  • 71
  • 1
  • 7
1

Just change file association of .js file to node.exe and you can run directly from explorer.

1) Right click on the file -> Select "Open with" -> Select "Choose another program"
2) Check box "Always use this app to open .js file"
3) Click "More apps" -> "Look for another app in PC"
4) Navigate to node.js installation directory.(Default C:\Program Files\nodejs\node.exe"
5) Click "Open" and you can just see cmd flashing

Now you will be able to run any .js files directly just by double clicking.

Note: Adding below code to the end of js file will be useful as it will keep the console open for a keystroke.

console.log('Press any key to exit');

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
Harikrishnan
  • 8,629
  • 9
  • 75
  • 119
0

For all stuck on how to start!

https://github.com/sethvincent/javascripting

Copy here incase link dies:

  1. Open node.js command prompt
  2. Make directory called javascripting by typing "mkdir javascripting"
  3. Change directory into the javascripting folder by typing "cd javascripting"
  4. Create a file named introduction.js by typing "touch introduction.js" OR FOR WINDOWS: "NUL > introduction.js"
  5. Open the file and type some javascript e.g. "Console.log('hello');"
  6. Save the file and check it runs by typing "javascripting verify introduction.js"
Fred Johnson
  • 2,109
  • 1
  • 23
  • 40
0

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"

Samuel Williams
  • 306
  • 3
  • 10
0

I had such problem for windows. And I decided it so: startApp.cmd:

@set JAVA_HOME=C:\jdk160_24
@set PATH=%JAVA_HOME%/bin;%PATH%
@cd /D C:\app\

@start /b C:\WINDOWS\system32\cscript.exe 
C:\app\start.js

and saved it cmd file in dir C:\scripts next file is runApp.bat:

C:\scripts\startApp.cmd
Dale K
  • 16,372
  • 12
  • 37
  • 62
0

Step For Windows

  1. press the ctrl + r.then type cmd and hit enter.
  2. now command prompt will be open.

  3. after the type cd filepath of file. ex(cd C:\Users\user\Desktop\ ) then hit the enter.

  4. please check if npm installed or not using this command node -v. then if you installed will get node version.
  5. type the command on command prompt like this node filename.js . example(node app.js)

C:\Users\user\Desktop>node app.js
vijayabalan
  • 71
  • 1
  • 3
0

Install node properly, that's it.

Now open terminal to path where your .js file is.

for example- if Javascript file(fileName.js) is at folder C:\Documents\Project1.

Just go to that folder, type node to check node is enable then type node fileName.js

Simple! Your work is done!!

Nitin Nema
  • 127
  • 2
  • 4
-1
c:\> node.exe %CD%\hello.js

%CD% captures the current directory under DOS

visitsb
  • 7
  • 1