What is NodeJS?

Node.js is a JavaScript runtime that is built on top of the Chrome V8 JavaScript engine, which allows you to translate JavaScript calls to native code. Node.js is primarily intended for building server-side JavaScript applications. Although there are also projects for writing desktop applications (Electron) and even for creating code for microcontrollers. But first of all, we are talking about Node.js as a platform for building web applications.

Node.js is an open source project whose source can be found at github.com.

Installation

To download, go to the official website https://nodejs.org/en/ . On the main page, we will immediately see two possible options for downloading: the latest version of NodeJS and the LTS version.

Let’s download the latest version. In my case, this is version 16.1.0. For Windows, the installer is an msi file. After launch, the installer program will open:

After a successful installation, you can enter the command node -v in the command line / terminal , and the current version of node.js will be displayed to us:

C:\Users\mycomputer>node
Welcome to Node.js v16.14.2.
Type “.help” for more information.

Versions of node.js for other operating systems along with the source can be found at https://nodejs.org/en/download/

Development tools

For development under Node JS, a simple text editor, in particular, Notepad ++, is enough. You can also use more sophisticated editors like Atom, Sublime, Visual Studio Code, or development environments that support Node.JS like Visual Studio or WebStorm.

REPL

After installing NodeJS, a tool such as REPL becomes available to us. REPL (Read Eval Print Loop) provides the ability to run JavaScript expressions on the command line or terminal.

So, let’s start the command line (on Windows) or the terminal (on OS X or Linux) and enter the command node . After entering this command, we can execute various JavaScript expressions:

C:\Users\mycomputer>node
Welcome to Node.js v16.14.2.
Type “.help” for more information.
> 10+3
13
> 10+30
40
>

Or use some JS function:

> console.log(“Hello NodeJS”);
Hello NodeJS
undefined
>

You can define your own functions and then call them, for example, squaring a number:

C:\Users\mycomputer>node
Welcome to Node.js v16.14.2.
Type “.help” for more information.
> 10+3
13
> 10+30
40
> function square(x){return x * x;}
undefined
> square(7)
49

If we enter something incorrectly, then the REPL will indicate an error:

Welcome to Node.js v16.14.2.
Type “.help” for more information.
> 10+3
13
> 10+30
40
> function square(x){return x * x;}
undefined
> square(7)
49
> squaree(10)
Uncaught ReferenceError: squaree is not defined
>

File execution

Instead of typing all the code directly into the console, it’s more convenient to put it in an external file. For example, let’s create a new directory on the hard drive, let’s say , C:\node\helloapp in which we will place a new app.js file with the following code:

 
console.log("Hello world");

At the command prompt, cd to the nodeapp directory, and then run the command:

node app.js

This command will execute the code from the app.js file:

C:\nodeapp>node app.js
Hello World

C:\nodeapp>