Node Tutorial Pt. 1 (2021)

Kiran Chauhan
6 min readDec 21, 2020

But the thing is, you got to walk before you can run. And in the same way, we’ve got to learn some basic building blocks before we can build these great programs. So that’s what we’re going to learn some basic building blocks out of which we’re going to build bigger programs. We’re going to try to do it quickly because it isn’t that exciting. But we’re going to try not to do it too quickly because we want everybody to be able to stay with us. Gregor Kiczales, Simple Data

Node.js or Node is a software that provides a way to write JavaScript on server-side. Although, nowadays you can write even mobile and desktop applications using Node (and JavaScript of course). But, the scope of these articles is to server-side only.

These articles assume that you know JavaScript and you’re familiar with web development in general. It is also good if you have some familiarity with Command Line Interface or CLI. CLI is available on all operating system in form of Terminal or Console or Command Prompt. I’ll use word terminal to refer this CLI utility. Also, I’m using Fedora operating system. So, all the instructions are written for the Fedora terminal only. You need to manage them, if you’re using any other operating system.

First of all, you need to download Node from its official website. You’ll see that there are two different versions of Node — LTS and Current. LTS means Long Term Support and Current means a version with latest features. If you’re planning to create the production application, you should go with LTS. But, if you’re experimenting or want to try the latest features, you might go with latest version.

Installing Node is easy and straightforward unless you’re planning to install it from source code. So, I’m not going to write HOWTO INSTALL instructions and assume that you know how to install it on your computer.

Once the installation is done, we can confirm the installation of Node via running following command in terminal.

$ node --version
v15.1.0

If you get version as output, then Node is installed properly on your computer. But, if you’re facing any problem while doing installation, please let me know.

One more thing before we go further, please run the following command in terminal.

$ npm --version
7.0.8

If you get version as output, then npm is installed properly on your computer. Now, you might have question like, what is npm? npm is the topic of future articles, for now just remember that when you install Node, it also install npm or Node Package Manager on your computer for good purpose!

So, now our environment is ready for the Node.

Note: --version or -v? There is no global rule on this. You must need to assist help command to know which option is supported. But, most of the time, you'll get both. If you're writing more than one character as option, you'll use -- and - with one character option. Hence, v with - and version with --. Again, this is not the global rule and you'll not see this pattern in all the command line applications (Try python -v or python --version on terminal if you have installed Python on your computer!).

Playing in the Shell

When you install Node on your computer, you also get something called Node shell or console. Console is the place where you can try out some lines of JavaScript code in live environment for debugging, testing, or for the experimenting purpose.

Let’s open the terminal and write node. You ll get console.

Welcome to Node.js v15.1.0.
Type ".help" for more information.
>

Yes, this > is the console. You can type any valid JavaScript code here and console will immediately execute it. This is quite helpful if you want to try our few lines of code or don't know how to use function or functionality. For example, I don't know what is the result of isNaN('hello'). I can type this line in console and it'll tell the result as follows:

> isNaN('hello');
true
>

You see, I asked a question to console and got the result. Again, this console environment is handy when you want to try-out couple of lines of code. Almost all the code that you write within file can be written here as well. But, this is not the recommended way except to experiment few lines of code.

While we’re here, let’s write hello, world program in console. You know how to write something in console in JavaScript, right? Correct! Using console.log() method. For example,

> console.log('hello, world');
hello, world
undefined
>

Well, we just wrote the famous hello, world program in Node!

Let’s try few more lines of code in console to become familiar with the environment. How about defining few variables such as p, r, n and then calculate the simple interest or si from these values.

> let p = 1000
undefined
> let r = 10
undefined
> let n = 1
undefined
> let si = p * r * n / 100
undefined
> si
100
>

Note: With ES6, you can now define variables using let keyword. The old var works just fine. But, now it is recommended to use let due to the notorious behavior of var.

You might notice, that the code we wrote so far in console is identical to what we normally write in client-side JavaScript. But, there are couple of differences between Node’s JavaScript and client-side JavaScript. For example, DOM or Document Object Model. In browser or in client-side JavaScript, you can write document.getElementById(). But in Node, if you write it, then you'll get an error as follows.

> document.getElementById('root');
Uncaught ReferenceError: document is not defined
>

The reason is obvious. Node does not run inside the browser and due to this, it doesn’t have document object. On the other hand, it run on server and due to this you have access to file system, networks, process, and so on which is not possible in browser JavaScript.

You can try out couple of examples in console and once you get the confident, you can exist it using .exit command or by pressing ctrl+c twice.

Writing in Files

But, the actual code goes inside the files ends with .js extension. Let's create a new hello-world.js file and write the following code.

console.log('hello, world');

Open the terminal where you saved this hello-world.js program. In order to run this program, following command is used.

$ node hello-world.js
hello, world

The command to run a Node program is node and then name of the file. Node assume that you'll write the code inside the file that will have .js extension. If this is the case, you can omit the extension while running the program as follows.

$ node hello-world
hello, world

Let me go further and tell you that Node just don’t care about the extension at all. Create a file with name hello.world and copy the same code we wrote in hello-world.js file.

console.log('hello, world');

Now, this program don’t have .js extension. Due to this, we need to write the program name with extension while running it from terminal as follows.

$ node hello.world 
hello, world

You can even give your name as the extension. But, don’t do that! I mean, you’ll not get the syntax highlighting in editor with that extension!

Let’s write one more program, where we’re going to print the greeting message to Bruce. Create a file with name greetings.js and open it in a text-editor. Let's first define a variable firstName using let as follows.

let firstName = "Bruce";

Then let’s console the greet message to Bruce as follows.

let firstName = "Bruce";

console.log('Hello, ' + firstName);

You know how we’re going to run this program, right?

$ node greet.js
Hello, Bruce

Even though, I mentioned that you can omit the extension while executing the Node program, if it has .js extension. But, I'm still going to write it and I would suggest you to do so.

Looks like we’re now little familiar with Node on how to write programs and run them. Try from your to write some programs and then let’s meet in next article.

P.S. You can also read the same article on my blog also.

--

--