Skip to main content

"Hello, World!" in Node.js

Introduction

The first program most people write when learning a new programming language is one form or another of the infamous "Hello, World!".

Preparation

What you'll need to know

To follow this tutorial you should be familiar with the basics of the command-line (also known as Terminal on OS X or Command Prompt on Windows). Below are some resources that will help you get up to speed on this topic:

  • Code Academy short course - Code Academy provide a short course on command line. It has an interactive prompt in the browser so you can get to grips with the syntax before diving into the CLI.

  • An introduction to Unix and Shell - The Interactive Telecommunications Program at NYU has a very interesting introduction to Unix, which is the precursor and model for Android, Apple iOS, Raspbian (Raspberry Pi), Linux and OSx operating systems. It is also a good overview of the history, philosophy and the anatomy of the shell.

Install NodeJS

You will need to download and install NodeJS. Download the installer for your particular operating system (OSX, Windows or Linux) from the NodeJS website and follow the instructions.

To test if NodeJS has been installed successfully:

  1. Open a command line prompt (Terminal or Command Prompt)
  2. And type the following: $ node -v

Do not type the `$`. This just tells you that everything following the dollar is a single line in the command line prompt.

You should see the version of NodeJS that you installed. Something like: v4.4.7. If you see an error then you may need to downloading and installing again.

Install other software (optional)

We recommend installing and using Atom text editor, because it is free, cross-platform and good for beginners to advanced programmers.

You will be editing JavaScript files throughout this tutorial, which can be done with almost *any* simple text editor you happen to have on your computer. This excludes Microsoft's Word, Apple's Pages or other word processing software, which don't count as simple and will add other unseen characters to your file.

Tutorial

To create and execute our first NodeJS application we simply (1) create a text file with the .js suffix, (2) edit the file and add some JavaScript and (3) pass this file to NodeJS using the command-line prompt.

(1) Create a directory and file

Our goal is to create a directory called hello-world and within it a file called hello.js. You can create a directory and a file using many methods but below are the instructions and an animation of how this is done using the command line.

$ cd Desktop
$ mkdir hello-world
$ cd hello-world
$ touch hello.js

The commands above explained:

  • cd [directory-name] - Change the current directory you are in
  • mkdir [directory-name] - Make a new directory in your current location
  • touch [file-name] - Create an empty file

nodejs1-terminal.gif

The result of this should be a directory and file on your desktop in this structure:

Desktop/
   └── hello-world/
      └── hello.js

(2) Edit file and add JavaScript

  • Open the file hello.js in your preferred text editor
  • Add the following code to the top of the file and save:
console.log("Hello, world of NodeJS!");

(3) Execute our hello.js script using NodeJS

  • Open a command-line prompt
  • Change directory so that you are inside Desktop/hello-world/
$ cd ~/Desktop/hello-world

TIP: When changing directory with the `cd` command (or using any command for that matter) you can use the tilde (`~`) to navigate to your home directory. e.g. `cd ~/Desktop`

- Pass your `hello.js` script to the `node` command ``` $ node hello.js ``` You should see the "Hello, world of NodeJS!" message printed out into your CLI prompt.

Below is an animation of this step.

Execute node script