Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a hello world javascript file in node.js

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.

like image 641
Joe Slater Avatar asked Mar 04 '13 16:03

Joe Slater


People also ask

How do I run a JavaScript file in node JS?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.

How do you write Hello World in node JS?

writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response. end('Hello World\n'); }). listen(8081); // Console will print the message console. log('Server running at http://127.0.0.1:8081/');

Can I run JavaScript in node JS?

Node. js is a popular open-source runtime environment that can execute JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web browser's JavaScript execution.


2 Answers

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

like image 180
Musa Avatar answered Sep 25 '22 08:09

Musa


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 
like image 34
Plato Avatar answered Sep 24 '22 08:09

Plato