Node.js tutorial: Hello world

This tutorial helps you get started with Node.js. It explains what you need to write your first Node.js program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Node.js program before this is the place to start. We have created this article, so you can get started with Node.js, and move on to our sms examples. If you are already familiar with Node.js, you can jump directly to one of the following SMS projects.

Node js sms examples:

Node js send sms with the HTTP rest api (code sample)
Node js send multiple sms with the HTTP rest api (code sample)
Node js schedule sms with the HTTP rest api (code sample)
Node js receive sms with the HTTP rest api (code sample)
Node js delete sms with the HTTP rest api (code sample)
Node js tutorial Hello world
Download the latest Node.js sms api library from Github

What is Node.js

Node.js is an open-source, cross-platform, runtime environment that executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

What is a Node.js Hello world program

A hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Node.js

To write your first program in Node.js:

  1. Download Node.js installer
  2. Install Node.js
  3. Test if Node.js is installed
  4. Write Hello World program script
  5. Use the console.log() command
  6. Run the code in the Command prompt
  7. Check the output
  8. Fix the errors

Prerequisites

Here is what you need to get started. To write your first computer program in JavaScript, you need a Windows computer, Visual Studio Code, Node.js and the example code presented below.

  • Windows 10 computer
  • Node.js
  • Ozeki Hello World example project

Download Node.js

To get started, you need to download the Node.js installer to your computer (Video 1). You may download Node.js from the following URL: https://nodejs.org/en/download/current/

Video 1 - Download Node.js

Download Node.js installer

To start coding in Node.js you first need to download the installer for the coding enviroment First, go to https://nodejs.org/en/download/current/. Click on the Downloads tab on the top of the page. This will bring you to the downloads page. Next, find the Windows Installer (.msi) option in the downloads (Figure 1). Click on the version intended for your operating system (32 bit or 64 bit). This will download Node.js installer on to your computer.

Figure 1 - Download Node.js

Install Node.js

To write your first Node.js program, you need to install visual studio code. The following video shows you how this installation can be done.

Video 2 - Install Node.js

Test Node.js

To test if Node.js is installed, you have to open the Command Prompt.

  1. Open the Start menu
  2. Type "cmd" into the search field
  3. Click Enter
  4. You will see a black window
  5. Type the code into the terminal what is in (Code 1)

node -v
	

Code 1 - Testing if Node.js is installed

Test if Node.js is installed

First, you need to open a command prompt. This can be done by typing "cmd" into the search bar on your taskbar. Test if Node.js is installed on your computer by typing "node -v" into the command prompt and pressing enter (Figure 2). If this command returns a version number, then Node.js is installed and in working order. After these steps, we can continue our journey to write our first 'Hello world!' program in JavaScript, and to run it in the Node.js envioment.

Figure 2 - Testing if Node.js is installed

Write your 'hello world' program in JavaScript

As we said a 'hello world' program is the easiest program you can write in any programing language. In JavaScript we use the console.log() command to print out messages to the console.

console.log("Hello world!");
	

Write the 'hello world' script

This video shows you how to create and write the 'hello world' script. We simply create a .js file and edit it using Notepad (Video 3).

Video 3 - Writing the 'hello world' script in JavaScript

Running your 'hello world' script in the Command Prompt

/*If you named your project otherwise you should replace the
'helloWorld' part with the name you gave to your file*/
node helloWorld.js
	

To run your code use the following code snippet above. If you placed your file somewhere else, your should use the cd command to go into that directory. It is enouch to copy the relative path and place it after the cd command.

Run the code in the Command prompt

Follow the instructions in this short video to run the Javascript code. Run your code by entering the command above into the Command Prompt (Video 4). By executing this code the Command Prompt will read "Hello world!".

Video 4 - Running the code in the Command Prompt

Output of the code

Run your script by opening up the Command Prompt again, and entering the following command: node helloWorld.js (Figure 3). The output of the code will then appear in the next line. When the script is executed, the Command Prompt will display the "Hello World!" message.

Figure 3 - The output of the code

Make an error and fix an error in Node.js

If you make a mistake in your code and you run it, the complier will show you the number of errors and it will maerk the palce where the error occured.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in JavaScript and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Node.js?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Error in the script

This video show you what happens when you make a mistake in the code. Javascript will not understand your code if the syntax is incorrect. This leads to a syntax error and your code fails to run, therefore the "Hello World!" message will not appear.

Error alert

When an error occours, the compiler alerts the developer that the code has an mistake in it. In the Command Prompt you can see a line of arrows pointing up below the faulty line (Figure 4). This is to indicate where the error happened. This is followed by an explanation to why the error happened.

Figure 4 - The compiler alerts the developer that the code has an error in it

Summary

This tutorial helps you get started with Node.js by showing you how to write your first Hello World code. We have tried to make this tutorial as simple as possible so that you can see that coding in Node.js is way not as complicated as it may seem for the first time. This knowledge helps you to understand other Node.js programs and gives you the ability to learn how to create your solution.

If you want to learn more about using Node.js programs, you can find more tutorials on the Ozeki website. Do not stop here, move on to the next document and study how to send SMS from Node.js.

Start the journey, download the Ozeki SMS Gateway now!

More information