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 running 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 simplest program you can write. It simply prints out the sentence: 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:
- Download Node.js installer
- Install Node.js
- Test if Node.js is installed
- Write Hello World program script
- Use the console.log() command
- Run the code in the Command prompt
- Check the output
- 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/
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.
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.
Test Node.js
To test if Node.js is installed, you have to open the Command Prompt.
- Open the Start menu
- Type "cmd" into the search field
- Click Enter
- You will see a black window
- Type the code into the terminal what is in (Code 1)
node -v
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 environment.
Write your 'hello world' program in JavaScript
As we said a 'hello world' program is the easiest program you can write in any programming 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).
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, you should use the cd command to go into that directory. It is enough 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!".
Output of the code
Run your script by opening up the Command Prompt again and enter 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.
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 mark the place where the error occurred.
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 shows 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 occurs, the compiler alerts the developer that the code has a 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.
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
- C# SMS tutorial: Hello world
- Visual Basic SMS tutorial: Hello world
- F# SMS tutorial: Hello world
- Java SMS tutorial: Hello world
- Javascript SMS tutorial: Hello world
- Node.js SMS tutorial: Hello world
- Kotlin SMS tutorial: Hello world
- C/C SMS tutorial: Hello world
- Delphi SMS tutorial: Hello world
- PHP SMS tutorial: Hello world
- Perl SMS tutorial: Hello world
- Python SMS tutorial: Hello world
- Python flask SMS tutorial: Hello world
- Ruby SMS tutorial: Hello world
- Tcl/Tk SMS tutorial: Hello world
- Go SMS tutorial: Hello world
- R SMS tutorial: Hello world
- Scala SMS tutorial: Hello world
- Objective C SMS tutorial: Hello world
- Tutorials for software developers