What is Node.js?

Node.js is a JavaScript runtime environment. The Node.js covers everything you require to run a program written in JavaScript.

Node.js evolved into existence when the JavaScript developers extended it from something you could simply run in your browser and on your machine as a standalone application.

Today you can use JavaScript for much more purpose than simply making websites interactive.

JavaScript now can perform tasks that other scripting languages like Python, PHP, etc. can do.

Your browser’s javaScript and Node.js both are run on the V8 JavaScript runtime engine. This JavaScript runtime engine converts your JavaScript code into faster machine code. Machine code is low-level code which the computer can understand without requiring to first interpret it.

Why Node.js?

Following is some formal definition which is given on the official Node.js website:

  • Node.js® is a JavaScript runtime developed on Chrome’s V8 JavaScript engine.
  • Node.js works on an event-driven, non-blocking I/O model that gives it lightweight and efficient.
  • Node.js’ packages, npm, is the biggest platform of open source libraries on the globe.

We previously explained the initial line of this explanation: “Node.js® is a JavaScript runtime environment created on Chrome’s V8 JavaScript engine.” Now let’s understand the other things so we can understand why Node.js is so popular.

I/O points to input/output. It can be anything ranging from reading/writing local files to creating an HTTP request to an API.

Blocks other functions because I/O takes time.

Suppose a situation where we request a backend DB for the details of two users, user1 and user2 and then publish them on the screen or console. The response to this request needs time, but both of the user detail requests can be taken out separately and at equal time.

What Can Node.js Do?

  • Node.js can produce the dynamic page content
  • Node.js can create, open, read, write, delete, and close server’s files
  • Node.js can handle form inputs
  • Node.js can also perform your DB tasks without using any server-side language

Blocking I/O

In the I/O blocking system, user2’s records request is not received until user1’s records are published to the screen.

If it was a web server, we would have to run a different thread for all user. But JavaScript is single-threaded (not literally, but it has a single-threaded event loop, which we’ll explain to next). Hence this would make JavaScript not so fully suited for multi-threaded tasks.

That’s where the non-blocking part comes in to handle this situation perfectly.

Non-blocking I/O

On the other hand, using a non-blocking request, you can initiate a data request for user2 without waiting for the response to the request for user1. You can initiate both requests in parallel.

In the Non-blocking I/O you can initiate a data request for both users without waiting for the response of other users.

The server can handle multiple requests at the same time, so the server is fulfilled the requirement of multi-threading for non-blocking I/O.

The event loop of JavaScript

The decoupling of the client from the response provides for the JavaScript runtime to do extra tasks while waiting for your asynchronous process to complete and their callbacks to perform.

JavaScript runtimes carry a message queue which holds a list of messages to be prepared and their connected callback functions. Those messages are queued in response to outside events (such as a mouse activity or getting the response to an HTTP request) given a callback function has been provided.

Below has given the event loop in step by step:

  1. Push main() over the call stack.
  2. Push console.log() over the call stack. This will runs soon and gets popped.
  3. Push setTimeout(3000) over the stack. setTimeout(3000) is a Node.js API. If we call it, we register the event-callback set. The event will wait for 3000 milliseconds, after that the callback function will be called.
  4. Next to registering it in the APIs, setTimeout(3000) gets popped from the call stack.
  5. Now the other setTimeout(0) takes registered in the same style. Now we have two Node APIs waiting to perform.
  6. After idle for 0 seconds, setTimeout(0) gets pushed to the callback queue, and the same thing repeats for setTimeout(3000).
  7. The functions wait in the callback queue for the call stack to be blank because only one statement can run at a time. Here is taken care of threading by the event loop.
  8. After last console.log() statement run, the main() gets popped from the call stack.


NPM stands for Node Package Manager which means maintaining your packages or modules for your application, it’s extremely sorted and managed way for manage your package or module dependencies, at the same time you can also achieve this goal by your convenient way, but npm is made exactly for it.

www.npmjs.com entertains thousands of available packages or modules to download and use.


Require performs three tasks:

  • It includes packages that get bundled with Node.js like the file system and HTTP from the Node.js API.
  • It loads third-party libraries like Mongoose and Express that you download from npm.
  • It lets you include your files and modularize the application.

Require is a function, and it allows a parameter path and returns module.exports.

Node Modules

A Node module is a reusable piece of code whose presence does not incidentally affect other code pieces.

You can also write your modules and apply them to any different application. Node.js has a collection of pre-built-in modules which you can apply without any additional installation.

V8 turbo-charged JavaScript by leveraging C++

V8 is an open-source JavaScript engine which has developed by the chromium project for Google Chrome and Chromium web browsers. The first version of both V8 engine and chrome was released at the same time on 2 September 2008 by The Lars Bak. The V8 engine has also been used on the server-side, for example in Couchbase, MongoDB and Node.js.

It is an open-source runtime engine written in C++.

JavaScript -> V8(C++) -> Machine Code

V8 executes a script named ECMAScript as defined in ECMA-262. ECMAScript was designed by Ecma International to regulate JavaScript.

V8 can also run standalone or can be embedded within any C++ application. It has hooks that let you write the C++ code which allows you to use in JavaScript.


Something that has occurred in our application that we can react to. There are two kinds of events in Node.js.

  • System Events: C++ core code which comes from a library called libuv.

Custom Events: JavaScript core codes.