A simple beginner’s guide to Node.Js and Npm (Part 1)

Varad Shevade
4 min readAug 5, 2020

Node.Js is a very vast topic. Yet I have tried to crunch some of its core concepts in 4 parts. This should be enough to get your foot in the door of what is called the world of Node.Js.

Prerequisites

  • Basic knowledge of Javascript.
  • Node.Js installed on your system

Download latest stable version from: https://nodejs.org/en/download/

Check node version with following command:

node -v

Check npm version with following command:

npm -v

What is Node.js?

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is an environment where your JavaScript code gets executed. Examples where JavaScript code gets executed are your web browsers like Chrome and Firefox.

Is it a server?

No it is not a server, but it comes with built-in modules like “http”, “net”, which makes it ideal for creating servers. It is also not a framework. Although there are popular web frameworks like Loopback, Express, Koa which are used to develop web api’s.

About Node.js

Node.Js is:

  • Non Blocking: Execution of synchronous I/O operations(like interaction with disk/network) can be handled in node.js asynchronously. Eg.:

Synchronous File Read operation:

const fs = require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

Asynchronous File Read operation:

const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
if (err) throw err;
});

Even though the 1st example appears to be easy, it is a blocking code, i.e. the execution will be blocked until the file is read. Now in contrast to this, the 2nd example is non blocking code. One of the most important advantages of non blocking over blocking, is that it allows for a much higher throughput, because the event-loop never blocks and continuously keeps executing.

  • Single Threaded: Here node.js handles one piece of code at a time. In a multi-threaded environment, concurrency is achieved by creating a new thread for every process intensive or network related task. Each of these threads take up some memory when they are employed, and with increase in number of threads, leads to increase in memory requirements, which in turn affects the concurrency and throughput. But in node.js, with the single-threaded approach and with the asynchronous paradigm, the memory requirement is drastically reduced. And concurrency is also maintained by utilizing all the available processor cores in your environment, where each core represents its own single thread with event-loop. The “cluster” module in node.js thus helps in achieving this concurrency.

What happens inside JS runtime?

At the core of JavaScript ecosystem there are 3 important entities which are: “Call Stack”, “Event Loop”, and the “Callback/Task Queue”.

Call stack” is simply a stack data structure, which tells us where we are in the program. When we enter a function, it is pushed on top of the stack, and when we return from the function, it pops out. Now remember the function that gets pushed into the stack may be blocking(synchronous) or non-blocking(asynchronous) depending on the coders requirement. And if the code is blocking, and the execution time is indefinite, then there is a serious performance issue. To solve this we have asynchronous callbacks, a simple concept where we have a piece of code which is executed asynchronously.

The “callback queues” handle these asynchronous functions. An asynchronous function like any other normal function also gets pushed to the call stack. It is popped when it’s execution is started. Being async in nature the callback of this async function is queued into the callback queue.

Here the “event loop” comes into picture. The task of the event loop is very simple. It checks whether the call stack is empty, and if it is empty, it will dequeue the callback function from the callback queue and push it onto the call stack. Finally the result of the async function is returned, when the callback function from the call stack is popped.

In Node.js, a library called “libuv” implements the event loop. libuv handles the file system, network, child processes, IPC, signals, streams, and other important operations. Here the event loop has a set of phases, each handling different events, and each phase has its own queue . The event loop moves from one phase to the next when the queue is exhausted of all the callbacks.

Detailed info at: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

Its Benefits?

  • Efficient memory utilization
  • Better throughput and scalability
  • Presence of concurrency

The most ideal situation for using node.js is in connection intensive scenarios like “live data streams”.

Yet with all of these benefits, node.js is not suitable for complex computation scenarios.

Steps to write a simple Node.js code

  • Create a new file & name it “myapp.js”
  • Paste following code in your file:
console.log("Hello World!");
var date = new Date();
console.log(`Today's date is ${date.toLocaleDateString()} & current time is ${date.toLocaleTimeString()}`);
  • Open your command prompt/terminal.
  • Go to the directory where you file is present.
  • In command prompt run following command: node myapp.js

Don’t forget to checkout the part 2 of the tutorial at:

--

--