Node JS Basic Examples With Node REPL (CLI) and Node IDE

In previous post, we have already spent some time on setting up Node JS Base environment. Now we will discuss about how to use Node CLI (Command Line Interface) or Node REPL(Read-Eval-Print Loop) to write and execute some basics Scripts and also discuss about Node JS IDE to write and execute simple Java Script files.

Brief Post Content:

  • Login and Exit into/from Node CLI
  • Execute Simple JavaScript Commands from Node CLI
  • Execute JS Files from Node CLI
  • Node IDE Setup
  • Develop Node JS project from Node IDE
  • Execute Node JS project from Node IDE

Login to/Exit from Node CLI

Open command prompt anywhere in your filesystem and execute “node” to login to Node CLI as show below;

node-cli-login-450x172

Use process.exit() command to exit from Node CLI as show below.

node-cli-login-450x172

Execute Simple JS Commands

We can execute any JavaScript code one by one from Node CLI as show below.

node-cli-login-450x172

Output: Hello JournalDEV users

Like this, we can run commands/statements from Node CLI directly and we can see the output on console.

Executing Java Script Files

Now we will discuss about “How to run JavaScript file that is ‘*.js’ files from Node CLI”.

If we want to run more statements at Node CLI, then we need to run them one by one. But if we write those statements into a JS file, we can run them with just single command. We need to use “node” command to run JS files from Node CLI.

Syntax:
nodejs-run-js-file-450x63
Here <JS-Filename> is our Java Script filename.

For example, write sample.js file.


var helloJDMessage = "Hello JournalDEV users";
console.log(helloJDMessage);

Use simple Text Editor or any Java Script editor to write this code.

node-cli-login-450x172

Open command prompt at “D:NodeJSExamples” and run “Sample.js” file as shown below.

node-cli-login-450x172

Here we can observe that we are able to print message to console when we run our Sample.js file with node command.

So far we have discussed about how to write simple JavaScript files by using some Text Editor. It’s ok to write some simple programs and for practice purpose.

However as a Developer, It’s not good practice to use Text Editors. We should use some Node IDE (Integrated Development Environment) or Tools to ease the Development process and also to reduce Development time and effort.

Now onwards, we are going to use a Node IDE known as “Eclipse Node.js IDE”. Eclipse Node.js IDE is simply known as Enide Studio. In Enide, “E” stands for Eclipse, “n” stands for Node JS and “ide” stands for IDE. We will use Enide latest version “Enide IDE 2014”.

There is another solution with Eclipse IDE is Nodeclipse. Nodeclipse is a set of Eclipse plug-ins for programming in JavaScript, CoffeeScript with Node JS Platform.

Enide IDE 2014 is a super set of Nodeclipse plugins plus some useful plugins like Angular JS Plugin, RestClient Tool, JSON Editor Plugin, MongoDB Shell Plugin, Gradle Plugin etc. So we are going to use “Enide IDE 2014” Eclipse IDE to develop our Node JS Examples.

If you are Java Developer, I guess you are already familiar with or used Eclipse IDE in your projects.

Enide Studio 2014 IDE Setup

Open your Eclipse IDE and click on “Eclipse Marketplace” from “Help” menu.

node-cli-login

It opens Eclipse Marketplace as shown in below image.

Eclipse-Marketplace-384x450

Type “enide” in “Find” text box and click on “Go” button. It searches for all available Eclipse software plugins with this name and displays as shown below:

node-cli-login
It shows “Enide Studio 2014” at first place. Click on “Install” button to install it.

Accept licence and click on “Finish” Button to start installation. Once installation is over, please restart your Eclipse IDE to see these updates. Right click on “Explorer” and select “New” menu option. Now you can see some Node JS Option like “Node JS Project” etc. These are installed as part of Enide Studio installation.

Enide-Menu-450x379

When we open “New” wizard, we can see “Nodeclipse” option as shown below.

node-cli-login

If we see those options that mean Enide Studio 2014 installation is done successfully.

If we install Enide Studio 2014 plugin after installing Node JS Base Platform, then this plugin will pick-up all Node JS Platform settings as shown below.

Select “Preferences” option from “Window” menu.

If you install Node JS Platform after this step, please check and set any missing properties in this Preferences wizard.

Develop Node JS Project with Enide Studio Node IDE

Create new Node JS Project by selecting “New >> Other…” from “File” menu.

Enide-Menu-450x379

Select Node.js Project from “New” wizard. Click on “Next” button.

Provide new project name as “sample” and also select “Template to use” as “Hello World”.

Node.js-project-template-450x424

Click on “Finish” button. It creates new Node JS Project “sample” and also download libraries.

It automatically changes to “Node” Eclipse Perspective as shown below.

Eclipse-Node-Perspective-450x308

Create a JavaScript file sample.js as shown below by right click on “sample” Node JS Project and select “New” >> “JavaScript file”.

Provide JavaScript file name and click on Finish button.

node-cli-login

Execute Node JS Project from Node IDE

Execute sample.js file from Enide Studio IDE. Select and Right Click on sample.js file. Select “Run As” >> “Node Application” option.

Observe the output at IDE Console.

Node.js-execute-js-output-450x303

Output: Hello JournalDEV Users!

Now we have some idea about how to use Enide Studio IDE to develop and run Node JS Applications with simple Java Script file.

We will discuss how to develop Node JS full-fledged application with Angular JS, Express JS, Mongo DB, RESTful WebServices etc in coming posts.

By admin

Leave a Reply

%d bloggers like this: