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.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.


Buy me a coffeeBuy me a coffee
π