Node.js: Code Outside the Browser / Node.js Internals and How it Works

Node, V8 and C++

๐ŸŽฏ Goal of This Note

Understand what Node.js is actually built from โ€” the underlying libraries and dependencies that make it work as a server-side JavaScript runtime. ๐Ÿง 


๐Ÿ—๏ธ Node.js Is Built on Dependencies

Node.js itself is essentially a combination of several key libraries working together. The two most important are:

  1. โšก V8 Engine

  2. ๐Ÿ”„ libuv


โšก 1. The V8 Engine

  • ๐Ÿš€ Node.js is a JavaScript runtime built on top of Google's V8 engine.

  • โš™๏ธ V8's job: converts JavaScript code into machine code that a computer's processor can actually execute.

  • โŒ Without V8, Node would have no way of understanding or running JavaScript code at all โ€” it's a completely fundamental building block of Node's architecture.

๐Ÿ’ก V8 alone, however, is not enough to build a full server-side framework โ€” it only handles turning JS into machine code, not things like file access or networking.


๐Ÿ”„ 2. libuv

libuv is an open-source library with a strong focus on asynchronous I/O (input/output).

๐Ÿ› ๏ธ What libuv Provides

  • ๐Ÿ–ฅ๏ธ Access to the underlying operating system โ€” including:

    • ๐Ÿ“ The file system

    • ๐ŸŒ Networking

    • โš™๏ธ Other OS-level features

  • โœจ Implements two of the most important features in Node.js:

    1. ๐Ÿ” The Event Loop

    2. ๐Ÿงต The Thread Pool

๐Ÿ”„ Event Loop vs. ๐Ÿงต Thread Pool (Brief Overview)

Component

Handles

๐Ÿ”„ Event Loop

"Easy," lightweight tasks โ€” executing callbacks, handling network I/O

๐Ÿงต Thread Pool

"Heavier" work โ€” file access, compression, and other CPU/resource-intensive tasks

๐Ÿ“– These two concepts will be explained in much greater detail in upcoming lectures โ€” this is just an introduction to what libuv is responsible for.


โš ๏ธ Node.js Isn't "Just JavaScript"

  • ๐Ÿ’ป libuv is written in C++, not JavaScript.

  • โšก V8 (the JS engine) also relies on C++ internally.

  • โœ… Conclusion: Node.js is actually a mix of C++ and JavaScript under the hood โ€” not pure JavaScript.


๐ŸŽจ The Magic: Abstraction

  • ๐Ÿค Node.js wraps all that C++ machinery and exposes it to you through plain JavaScript.

  • โœ๏ธ You write JavaScript only โ€” e.g. fs.readFile() โ€” while C++ code in libuv does the real work behind the scenes.

  • ๐Ÿ™Œ You never touch C++ yourself.

๐Ÿ’ก Without this abstraction layer, building server apps would mean writing C++ directly โ€” far harder and less accessible.


๐Ÿ“š Other Supporting Libraries in Node.js

Besides V8 and libuv, Node also depends on several other specialized libraries:

Library

Purpose

๐ŸŒ http-parser

Parsing HTTP data

๐Ÿ” c-ares (or similar)

Handling certain DNS-related requests

๐Ÿ”’ OpenSSL

Cryptography (used for security-related operations, like HTTPS)

๐Ÿ“ฆ zlib

Compression

๐Ÿ’ก These libraries are less critical to deeply understand at this stage โ€” they're mentioned mainly for completeness, to show the full picture of what Node.js is built from.


๐Ÿงฉ Putting It All Together

When all these pieces โ€” โšก V8, ๐Ÿ”„ libuv, ๐ŸŒ http-parser, ๐Ÿ” c-ares, ๐Ÿ”’ OpenSSL, ๐Ÿ“ฆ zlib, and others โ€” are combined, the result is Node.js: a runtime ready to be used for building real-world, server-side applications. ๐Ÿš€