Node.js Introduction

Last Updated : Oct 2026


Node.js is a JavaScript runtime environment that allows you to run JavaScript code on the server side. It is open-source and free to use developed by Ryan Dahl in 2009.

Before Node.js came out in 2009, JavaScript lived only inside web browsers (like Chrome, Firefox, or Safari). Browsers provided an engine to run JS, but restricted it to the webpage for security—meaning JS couldn't touch your computer's files, operating system, or network ports directly.

Ryan Dahl took Google Chrome's lightning-fast C++ JavaScript engine—V8—and wrapped it inside a C++ program. He added low-level capabilities so JavaScript could run directly on your operating system as a server-side language.

       Browsers                     Node.js
+--------------------+       +--------------------+
| JavaScript Engine  |       | JavaScript Engine  |
|      (V8, etc.)    |       |        (V8)        |
+--------------------+       +--------------------+
|  DOM, Window, Web  |       | File System, OS,   |
|   APIs (Browser)   |       | HTTP, Network APIs |
+--------------------+       +--------------------+

Note: Node.js is not a framework or a programming language. It is a runtime environment that lets JavaScript run outside the browser.


Node.js Working Principles

Traditional server frameworks (like PHP or Ruby on Rails) typically use a multi-threaded synchronous model:

  1. A user requests data from the server.
  2. The server spawns a dedicated thread for that user.
  3. If that thread reads a file or queries a database, it waits (blocks) until the operation finishes before serving the next line of code or another user.

Node.js does the exact opposite using a Single-Threaded Non-Blocking I/O model:

  • Single-Threaded: It runs all your JavaScript code on one main thread.
  • Non-Blocking I/O: When your app needs to read a file, make a database query, or fetch data over the network (Input/Output operations), Node offloads that task to the background operating system threads and moves on to execute the next line of code immediately.
  • Event-Driven: Once the background task finishes, Node receives a signal (an event) and handles the result.

Let's take a look at a simple real world example:

Imagine a busy restaurant with one waiter (Node's main thread):

  • Synchronous (Blocking) Model: The waiter takes Order A, walks to the kitchen, and stands there doing nothing until the chef finishes cooking Dish A. Only then does the waiter take Order B. (Extremely inefficient!)
  • Asynchronous (Non-Blocking) Model: The waiter takes Order A, drops it in the kitchen, and immediately walks to the next table to take Order B. When Dish A is cooked, the kitchen rings a bell (an Event), and the waiter serves Dish A whenever they finish their current small action.

V8, Libuv, and the Event Loop

Two main C++ engines work together under Node's hood:

  1. V8 Engine: Converts your JavaScript into machine code that your CPU understands.
  2. Libuv: A C library that provides Node with access to thread pools, network sockets, and file operations. It manages the Event Loop.

Node.js Code Execution

  1. Call Stack: Where JavaScript keeps track of function calls. (Last In, First Out).
  2. Background Node APIs / Libuv: Where long tasks (reading files, timers, database calls) are sent so they don't block the stack.
  3. Task Queue (Callback Queue): Where completed background tasks wait to be processed.
  4. The Event Loop: A continuous loop that asks one question: "Is the Call Stack empty?" If yes, it pulls the first callback from the Task Queue and pushes it onto the Call Stack to execute.

Summary:

The Event Loop is a single-threaded loop that waits for events to happen. When an event happens, the Event Loop schedules the callback to be executed.

When the callback is executed, the Event Loop repeats the process until no more events are waiting to happen.


Node.js Installation

You can download Node.js(LTS) from the official website: Click Here

Once you have Node.js installed, you can start working with Node.js by opening a terminal and typing node to start the REPL (Read-Eval-Print Loop).

node

To exit the REPL,type .exit or simply press Ctrl+D (one time) or Ctrl+C (two times)` .

  • Check your Node.js version by typing node -v in the terminal.
node -v
  • Run Js file by typing
node your_filename.js
  • Run Js file with auto reloading when file changes
node --watch your_filename.js


Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR