Introduction to Node.js
Node.js is an open-source, cross-platform JavaScript runtime environment built on Google Chrome's V8 engine that executes JavaScript code outside a web browser.
The Core Idea: Escaping the Browser Sandbox
Historically, JavaScript lived exclusively inside web browsers (Chrome, Firefox, Safari). To protect users from malicious websites, browsers enforce a strict security sandbox: JavaScript cannot read or write to your local hard drive, inspect hardware specifications, or bind to network ports.
In 2009, Ryan Dahl created Node.js by taking Google's high-performance V8 JavaScript engine out of Chrome, wrapping it in C++ bindings, and pairing it with libuv (an asynchronous event-driven I/O library). This allowed JavaScript to interact directly with the underlying operating system.
┌────────────────────────────────────────────────────────┐
│ Your JavaScript Code │
└───────────────────────────┬────────────────────────────┘
│
┌─────────────┴─────────────┐
▼ ▼
┌───────────────────────────┐ ┌───────────────────────────┐
│ Google V8 Engine │ │ Node.js Core C++ APIs │
│ (Parses & Compiles JS) │ │ (node:fs, node:http...) │
└─────────────┬─────────────┘ └─────────────┬─────────────┘
│ │
└─────────────┬───────────────┘
▼
┌───────────────────────────┐
│ libuv │
│ (Event Loop & Thread Pool)│
└─────────────┬─────────────┘
▼
┌───────────────────────────┐
│ Operating System │
│ (Disk, Sockets, Kernel) │
└───────────────────────────┘Key distinction: Node.js is not a programming language, and it is not a framework. It is an execution host (runtime) for JavaScript.
Two Essential Examples
Operating System & Filesystem Access
Browser JavaScript cannot touch the file system. In Node.js, built-in modules like node:os and node:fs/promises speak directly to the OS kernel.
os.cpus()retrieves hardware thread count directly from host kernel tables.fs.writeFileinvokes POSIX system calls (open,write,close) to persist bytes to disk.
A Standalone HTTP Web Server in 6 Lines
Node.js doesn't require an external application server (such as Apache or NGINX) to run your code. The runtime itself creates and manages TCP sockets and HTTP connections.
http.createServerregisters a callback triggered whenever a client socket completes an HTTP request.- Thousands of incoming requests can be served asynchronously on a single thread without allocating a dedicated OS thread per connection.
Browser JavaScript vs. Node.js at a Glance
| Aspect | Web Browser | Node.js |
|---|---|---|
| Global Root | window, document | globalThis, process |
| File System Access | Sandboxed (no arbitrary disk read/write) | Full POSIX filesystem access (node:fs) |
| Networking Role | Client-only (fetch, WebSockets with CORS) | Server & Client (raw TCP, UDP, HTTP servers) |
| DOM & UI | Direct DOM tree rendering & styling | None (headless server & CLI execution) |
| System Primitives | Restricted to tab environment | Environment variables, signals, child processes |
Try It: In-Browser Node.js Runtime
Edit JavaScript on the left and click Run Code (or press ⌘+Enter) to observe real-time output in the terminal on the right.
Ready to execute. Click "Run Code" to evaluate your JavaScript.
Node.js executes your JavaScript on a single thread. While asynchronous I/O (database queries, network requests, disk reads) does not block this thread, heavy synchronous CPU computations (e.g. encrypting large payloads, calculating primes, or synchronous JSON parsing) freeze the entire process. While blocked, no other connected client can be served.