# Node.js — The JavaScript Runtime Built on V8 > Node.js is the open-source JavaScript runtime that enables server-side JavaScript execution. Built on Chrome V8, it uses an event-driven, non-blocking I/O model that makes it lightweight and efficient for building scalable network applications. ## Install Save as a script file and run: # Node.js — The JavaScript Runtime Built on V8 ## Quick Use ```bash # Install Node.js # macOS brew install node # Or use version managers curl -fsSL https://fnm.vercel.app/install | bash fnm install --lts # Run JavaScript node -e "console.log('Hello from Node.js', process.version)" # Create a simple HTTP server node -e " const http = require('http'); http.createServer((req, res) => { res.end('Hello World'); }).listen(3000, () => console.log('http://localhost:3000')); " ``` ## Introduction Node.js revolutionized web development by bringing JavaScript to the server. Created by Ryan Dahl in 2009, it enabled full-stack JavaScript development — one language for both frontend and backend. Its non-blocking, event-driven architecture makes it exceptionally efficient for I/O-heavy workloads like web servers, APIs, and real-time applications. With over 117,000 GitHub stars, Node.js is one of the most popular runtime environments in the world. It powers the backends of Netflix, PayPal, LinkedIn, Uber, NASA, and millions of applications. The npm registry hosts over 2 million packages, making it the largest software registry in existence. ## What Node.js Does Node.js executes JavaScript outside the browser using the V8 engine. Its event loop handles thousands of concurrent connections on a single thread without the overhead of thread-per-connection models. It provides built-in modules for file system access, networking, HTTP, streams, cryptography, and child processes. ## Architecture Overview ``` [JavaScript Application Code] | [Node.js Runtime] | +-------+-------+-------+ | | | | [V8 [libuv] [Built-in Engine] Event Modules] JS loop, http, fs, execution async I/O path, crypto thread stream, net pool | [Event Loop] Single-threaded Non-blocking I/O Callbacks, Promises, async/await | [npm / node_modules] 2M+ packages Largest software registry ``` ## Self-Hosting & Configuration ```javascript // Modern Node.js HTTP server with ES modules import { createServer } from "node:http"; import { readFile } from "node:fs/promises"; const server = createServer(async (req, res) => { if (req.url === "/api/data") { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ message: "Hello", timestamp: Date.now() })); } else if (req.url === "/") { const html = await readFile("./index.html", "utf-8"); res.writeHead(200, { "Content-Type": "text/html" }); res.end(html); } else { res.writeHead(404); res.end("Not Found"); } }); server.listen(3000, () => { console.log("Server running at http://localhost:3000"); }); ``` ```json // package.json { "name": "my-app", "type": "module", "engines": { "node": ">=20" }, "scripts": { "start": "node server.js", "dev": "node --watch server.js" } } ``` ## Key Features - **V8 Engine** — fast JavaScript execution with JIT compilation - **Event Loop** — non-blocking I/O for high concurrency on a single thread - **npm Ecosystem** — 2M+ packages in the largest software registry - **ES Modules** — native import/export support alongside CommonJS - **Built-in Watch Mode** — --watch flag for auto-restart during development - **Test Runner** — built-in node:test module (Node 20+) - **Worker Threads** — multi-threading for CPU-intensive tasks - **WebStreams** — standard web-compatible stream APIs ## Comparison with Similar Tools | Feature | Node.js | Deno | Bun | Python | Go | |---|---|---|---|---|---| | Language | JavaScript/TS | JavaScript/TS | JavaScript/TS | Python | Go | | Package Manager | npm/pnpm/yarn | deno add | bun | pip/uv | go mod | | Speed | Fast | Fast | Very Fast | Moderate | Very Fast | | Ecosystem | Massive (2M+) | Growing + npm compat | npm compat | Large | Moderate | | TypeScript | Via transpiler | Native | Native | N/A | N/A | | Concurrency | Event loop | Event loop | Event loop | asyncio/threads | Goroutines | | Maturity | 15+ years | 6 years | 3 years | 30+ years | 15 years | ## FAQ **Q: Node.js vs Deno vs Bun — which should I use?** A: Node.js for the largest ecosystem and maximum compatibility. Deno for security-first with TypeScript. Bun for speed. Most production systems use Node.js due to its maturity and ecosystem. **Q: Is Node.js good for CPU-intensive tasks?** A: Node.js excels at I/O-bound work (APIs, web servers, streaming). For CPU-intensive tasks, use Worker Threads, child processes, or consider Go/Rust. Node.js is not ideal for heavy computation on the main thread. **Q: How do I manage Node.js versions?** A: Use fnm (Fast Node Manager), nvm, or Volta. These tools let you install multiple Node.js versions and switch between them per-project. **Q: Is Node.js single-threaded?** A: The event loop is single-threaded, but I/O operations run on a thread pool (libuv). Worker Threads provide true multi-threading for CPU work. In practice, Node.js handles massive concurrency efficiently. ## Sources - GitHub: https://github.com/nodejs/node - Documentation: https://nodejs.org/docs - Created by Ryan Dahl (2009) - License: MIT --- Source: https://tokrepo.com/en/workflows/f83bce6e-3712-11f1-9bc6-00163e2b0d79 Author: Script Depot