37

I have hello_world.js file located on my computer at the following location -

C:\Users\Ankur\Downloads\NodeJS\working

It has the following code-

console.log("Hellow World");

I was reading a beginners tutorial here and it says I should execute my javascript file through node.js. Now I have no idea what to do. How would I do this.

When I do this ... enter image description here

I get nothing. What should I be doing to run the hello world successfully. None of the tutorials seem to be showing what needs to be done.

I am very new to this so it is probably something that is very obvious to you guys.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Joe Slater
  • 2,483
  • 6
  • 32
  • 49
  • 1
    There are so many postings available at SO on this , here is the one which I often refer http://stackoverflow.com/questions/6737824/how-to-run-a-hello-js-file-in-node-js-on-windows Review the answer posted by @kcbanner which exactly solves your problem . – Aravind.HU Mar 04 '13 at 17:11
  • 1
    What if I had to require a module, say like bluebird? – jgr0 Sep 08 '16 at 18:53
  • Syntax Error! This is not a valid "Hello World" program. – davidhigh Jan 29 '20 at 23:47

7 Answers7

37

Use Node.js command prompt, then type in node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

Musa
  • 96,336
  • 17
  • 118
  • 137
21

looks like you are in a node console already. you typed node which correctly started node (so your path is fine). Now you are telling node to interpret

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

as javascript which it doesn't like.

You should be able to type console.log('hello world'); here and see it appear.

To run your file, quit out of the node interpreter (i think control-X, maybe control-C), and at your C:> prompt, THEN type

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

Alternately,

cd C:\Users\Ankur\Downloads\NodeJS\working\
node hello_world.js
Plato
  • 10,812
  • 2
  • 41
  • 61
3

You have entered the node console, by typing node into a command prompt, and then tried to execute node from within itself. Just type node c:\etc\...\ from the command prompt, not the node shell.

Press: [Start]->[Run]->[c][m][d]

And enter command: node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

Alternatively, use sublime editor, and install a node js build system, and just open the file you want to run, and press the build shortcut, getting the output in the console.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
1
  1. Open CMD.
  2. Type "cd C:\Users\Ankur\Downloads\NodeJS\working" and hit Enter
  3. Type "node hello_world.js" and hit Enter

This should work out !! IMPORTANT: Dont type node before the point number 3.

sarathmojo
  • 63
  • 6
1

Follow these three easy steps:

  • Go to your working directory

    C:\Users\Ankur\Downloads\NodeJS\working 
    
  • Open cmd there and type

    node hello_world
    
  • Press Enter

zx485
  • 28,498
  • 28
  • 50
  • 59
Ahmed Atia
  • 11
  • 1
0

Alternative. Open Nodejs "The thing with a green icon". Copy the code that you would like to be executed from the file that you have

paste it in nodejs then press enter

nodejs understands js code so it will execute what you have written and output hello world undefined

What you have descripbed opens the file that you have and then executes it.

Oliver Tembo
  • 305
  • 3
  • 6
-1

Open Windows cmd and type

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

You should use node from system interpreter (bash/DOS cmd), what you are running is C:\Program Files\nodejs\node.exe without any arguments.

user568109
  • 47,225
  • 17
  • 99
  • 123