Node.js helloworld example in Eclipse & Command Prompt in windows.

In this tutorial, let us see how to create & run hello world node application in command prompt and eclipse ide. Also we will see how to install & setup node.js in windows os. Node.js a JavaScript runtime built on Chrome’s V8 JavaScript engine allows you to develop server side applications using javascript. It has many inbuilt libraries which eases writing of server side applications. For eg. you can create an application which serves as a web server with 5 lines of code. Usually Javascript code is executed by client browser. But in Node.js, javascript code is executed by node.js engine installed at server which  provides an event-driven architecture.

Now let us see how to install node.js.
Download node.js for windows at https://nodejs.org/en/

Run the exe file which will install the node.js which comes with npm (node package manager) 

Now You should be able to run both node and npm  commands on command prompt

Suppose node.js installed on C:\Program Files\nodejs

then npm will be located on C:\Program Files\nodejs\node_modules\npm\bin

note : nodejs should be in windows path. You can check it with path command.

Test both npm and node installation by entering the commands at command prompt as given below

c:\>npm 

Usage: npm <command>

where <command> is one of:
access, add-user, adduser,…

c:\>node

>

Now you will be able to run any node application.

Now let us create and run a node application using command prompt

Node Application in Command Prompt

Make a folder

c:\>mkdir node_example

c:\node_example>create a text file and write the below code

and save it as helloworld.js

Now run the node application by running the below command on command prompt

c:\node_example>node helloworld.js

nodejs helloworld command prompt

Now let us see How to create node.js application in Eclipse
Node Application in Eclipse

Before creating a node.js project, we need to install nodeclipse plugin for eclipse so that we can develop and run node.js application in eclipse ide.

To install nodeclipse plugin, follow the below steps

Open Eclipse and  access  the menu Help->Eclipse Marketplace

Eclipse nodeclipse

type nodeclipse in the  find box and search which will list the nodeclipse, the eclipse plugin for Node.js

Now select install and restart eclipse after the installation is complete.

At  command prompt, run the below command

C:\node_example>npm install -g nodeclipse

Output of the above command:

Now plugin installation is complete.

To create  node  project in eclipse, create a folder (for eg. node_example) where you want to create node application and type the below command on the command prompt

C:\node_example>nodeclipse -p

Now Add the above project in Eclipse

File -> Import… -> General / Existing Projects into Workspace
and enter project directory: C:\node_example

node helloworld eclipsenode helloworld eclipse1 node helloworld eclipse2

Click on finish

Project Structure

node eclipse project structure
Now Run the node application by right clicking  on the helloworld.js file and selecting  Run on Server->Node Application

 

node helloworld eclipse3

node app eclipse output

 

References

  1.  https://en.wikipedia.org/wiki/Node.js

2.  node.js

You may also like

Leave a Reply

Be the First to Comment!