Skip to content
/
1/280
Module 1: Node.js Fundamentals•Concept 01 of 280

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.

Runtime ArchitectureC++ & OS Layer
┌────────────────────────────────────────────────────────┐
│                   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

Example 1

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.

// system-check.mjs
import os from 'node:os';
import fs from 'node:fs/promises';
// 1. Inspect physical hardware
console.log(`OS: ${os.type()} | Cores: ${os.cpus().length} | Free RAM: ${(os.freemem() / 1e9).toFixed(1)} GB`);
// 2. Write and read directly on disk
await fs.writeFile('server.log', `Booted at ${new Date().toISOString()}\n`);
const log = await fs.readFile('server.log', 'utf-8');
console.log('Saved Log:', log.trim());
  • os.cpus() retrieves hardware thread count directly from host kernel tables.
  • fs.writeFile invokes POSIX system calls (open, write, close) to persist bytes to disk.
Example 2

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.

// server.mjs
import http from 'node:http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', uptime: process.uptime() }));
});
server.listen(3000, () => console.log('Server running on port 3000'));
  • http.createServer registers 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

AspectWeb BrowserNode.js
Global Rootwindow, documentglobalThis, process
File System AccessSandboxed (no arbitrary disk read/write)Full POSIX filesystem access (node:fs)
Networking RoleClient-only (fetch, WebSockets with CORS)Server & Client (raw TCP, UDP, HTTP servers)
DOM & UIDirect DOM tree rendering & stylingNone (headless server & CLI execution)
System PrimitivesRestricted to tab environmentEnvironment 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.

Node.js v22 REPL Lab
Examples:
main.mjsJavaScript Editor
1
2
3
4
5
6
7
8
9
10
11
⌘+Enter to Run11 lines
Exit: 0 • 0ms
>
Ready to execute. Click "Run Code" to evaluate your JavaScript.
Process PID: 10421 output line(s)
The Golden Rule: Don't Block the Event Loop

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.

Check Your Understanding

0/3 answered
1. What is Node.js?
2. Why can Node.js read files and open TCP ports while browser JavaScript cannot?
3. What happens if a Node.js HTTP server executes a 10-second synchronous calculation during a request?