NODE: Introduction to Node.js

Node.js is a runtime environment that allows JavaScript to run on the server side, outside of the browser. Built on Chrome's V8 engine, it enables scalable and high-performance backend applications such as APIs, real-time services, and CLI tools.

JavaScript was originally created to run inside a web browser. Node.js changed that by bringing JavaScript to the server — making it possible to use a single language across the entire stack, from frontend to backend.

Node.js is not a framework or a library. It is a runtime environment that executes JavaScript code outside the browser using Google Chrome's V8 engine.


What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side to build backend services, APIs, real-time applications, and command-line tools.

md
Browser                              Node.js
--------                             -------
JavaScript runs in browser           JavaScript runs on server
DOM, window, document APIs           File system, HTTP, OS APIs
Sandboxed execution                  Full system access
User interaction focus               Backend logic focus

At its core, Node.js bridges the gap between JavaScript and server-side programming.


Why Node.js?

Node.js was created to solve a specific problem: blocking I/O in traditional server architectures.

Traditional servers create a new thread for each incoming request, which consumes memory and limits scalability. Node.js uses a different model — non-blocking, event-driven I/O — that handles many concurrent connections efficiently with a single thread.

md
Traditional Server (Blocking)
------------------------------
Request 1 --> Thread 1 --> Wait for DB --> Respond
Request 2 --> Thread 2 --> Wait for DB --> Respond
Request 3 --> Thread 3 --> Wait for DB --> Respond
...
(Many threads, high memory usage)

Node.js Server (Non-Blocking)
------------------------------
Request 1 --> Async I/O --> Callback when done
Request 2 --> Async I/O --> Callback when done
Request 3 --> Async I/O --> Callback when done
...
(Single thread, event loop handles callbacks)

This makes Node.js ideal for:

  • REST APIs with high concurrency
  • Real-time applications (chat, notifications, live updates)
  • Streaming services (audio, video, file uploads)
  • Microservices and lightweight backends

The V8 Engine

Node.js is powered by the V8 JavaScript engine, developed by Google for Chrome. V8 compiles JavaScript directly to native machine code rather than interpreting it, which makes execution extremely fast.

md
JavaScript Source Code
        |
        v
   +----------+
   |    V8    |  (Compilation + Optimization)
   +----------+
        |
        v
Native Machine Code
        |
        v
    Execution

V8 handles:

  • Just-In-Time (JIT) compilation of JavaScript
  • Garbage collection and memory management
  • Optimization of hot code paths

The Node.js Event Loop

Node.js is single-threaded, meaning it runs on one main thread. Yet it can handle thousands of concurrent connections. This is possible because of the Event Loop.

The Event Loop is the mechanism that allows Node.js to perform non-blocking operations by offloading work to the operating system and then picking up the result when it is ready.

md
           +----------------------------+
           |          Node.js           |
           |                            |
           |   +--------------------+   |
           |   |    Event Loop      |   |
           |   |                    |   |
           |   |  timers            |   |
           |   |  I/O callbacks     |   |
           |   |  poll              |   |
           |   |  check (setImm.)   |   |
           |   |  close callbacks   |   |
           |   +--------------------+   |
           |          |                 |
           |   +-------+--------+       |
           |   |  libuv thread  |       |
           |   |  pool (async)  |       |
           |   +----------------+       |
           +----------------------------+

Phases of the Event Loop:

PhaseDescription
timersExecutes setTimeout and setInterval callbacks
I/O callbacksHandles most async I/O completion callbacks
idle / prepareInternal use by Node.js
pollRetrieves new I/O events and executes their callbacks
checkExecutes setImmediate callbacks
close callbacksExecutes close event callbacks (e.g., socket close)

Understanding the event loop is essential for writing correct, performant Node.js code.


What is npm?

npm (Node Package Manager) is the default package manager that ships with Node.js. It is the largest software registry in the world, hosting over a million open-source packages.

With npm you can:

  • Install third-party libraries and frameworks
  • Manage project dependencies
  • Publish your own packages to the registry
  • Run scripts defined in your project

bash
# Check installed versions
node -v
npm -v

npm organizes dependencies in a package.json file, which serves as the manifest for your project.


Installing Node.js

The recommended approach is to download the LTS (Long Term Support) version from the official website, which provides the most stable release suited for production use.

Installation steps:

  1. Visit nodejs.org
  2. Download the LTS version for your operating system
  3. Run the installer — npm is included automatically

Verify your installation:

bash
node -v
# e.g., v20.11.0

npm -v
# e.g., 10.2.4

Run a quick test:

bash
node -e "console.log('Node.js is working')"


Node.js Architecture Overview

md
+---------------------------------------------------+
|                  Your Application                 |
+---------------------------------------------------+
|               Node.js Standard Library            |
|  (http, fs, path, events, stream, crypto, ...)    |
+---------------------------------------------------+
|                    Node.js Bindings                |
+---------------------------------------------------+
|        V8 Engine         |        libuv            |
|  (JS execution, GC)      |  (async I/O, threads)  |
+---------------------------------------------------+
|              Operating System / Kernel            |
+---------------------------------------------------+

Key components:

  • V8 — compiles and executes JavaScript
  • libuv — provides the event loop, async I/O, and thread pool
  • Node.js Bindings — bridges JavaScript code to native C++ APIs
  • Standard Library — built-in modules for common operations

Key Characteristics of Node.js

CharacteristicDescription
Single-threadedRuns on one main thread using the event loop
Non-blocking I/OAsync operations do not block the main thread
Event-drivenExecutes callbacks in response to events
Cross-platformRuns on Windows, Linux, and macOS
FastV8 compiles JS to native code at runtime
ScalableHandles many concurrent connections efficiently

When to Use Node.js

Node.js excels in workloads that are I/O-bound rather than CPU-bound.

Good use cases:

  • RESTful APIs and GraphQL servers
  • Real-time applications (WebSocket, chat, gaming)
  • Streaming data pipelines
  • Microservices architectures
  • Command-line tools and build systems
  • Serverless functions

Less ideal for:

  • CPU-intensive tasks (image processing, video encoding)
  • Heavy mathematical computations
  • Applications requiring true parallelism at the core logic level

Node.js in the Ecosystem

Node.js does not exist in isolation. It is the foundation for a rich ecosystem:

md
+-------------------------------------------+
|              Node.js Ecosystem             |
|                                            |
|  Frameworks:   Express, Fastify, NestJS    |
|  Databases:    Mongoose, Prisma, Sequelize |
|  Testing:      Jest, Mocha, Chai           |
|  Build:        Webpack, Vite, esbuild      |
|  Runtime:      Deno, Bun (alternatives)    |
+-------------------------------------------+

The npm registry provides access to thousands of ready-to-use packages, dramatically accelerating development.


Final Thoughts

Node.js brought JavaScript beyond the browser and made full-stack JavaScript development a practical reality. Its non-blocking, event-driven architecture makes it a powerful choice for building modern, scalable backend services.

Understanding Node.js at its core — the V8 engine, the event loop, and the npm ecosystem — gives you the foundation to build anything from a simple API to a complex real-time platform.

The server is now your domain as a JavaScript developer.