There are two primary ways to use Node.js on your development machines: by using the Node Shell or by saving JavaScript to files and using node to execute them.

Node Shell

The Node Shell a.k.a Node REPL ( Read Eval Print Loop) , is a utility to enable developers to quickly test commands in Node.  It is extremely useful in situations when you need to test a command or would like to explore how a command works.  It provides you the ability to quickly use the REPL and type in a command and see what happens.

To launch the node shell, you simply open a terminal window type node followed by any command.  In the example below we’ll use the REPL to print out Geek.I.Am to to console.

The first line of the output is the result of the code you just executed. In this case, you use the Node global variable console and its log function to print out Geek.I.Am . The output of this statement is, predictably, Geek.I.Am, which is printed for you.

The last line of the output is always the resulting value of the preceding statement. Every statement, function call, or expression has a value associated with it, which is printed out in the Node shell for you. If there is no evaluated expression value or the called function does not return any particular value, the special value undefined is returned instead.

To exit the REPL , simply use CTRL + D

Once you’re in the node REPL environment, you can write as many commands you choose. You can write entire applications within the environment,  the only downside is that it is ephemeral and your application is only live the duration that it is in the REPL environment.

Developing and executing JavaScript Files

The most common method of developing node applications is to do so by creating JavaScript files.  You can simply use your favourite text editor to write JavaScript code into a file, you can then use node to execute the code  via the terminal.

To demonstrate this, open any Text Editor i.e. NotePad, NotePad++,  Gedit, nano  or any of your other favourite text editor.  Insert the code below.

Save the file to a directory and name it hellogeek.js . You can now execute this file via the terminal by navigating to the directory and typing

You should see the output

You are not in the Node Shell, so you don’t get any information on the return values of the code executed.

 

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π