Node.js is an open-source, cross-platform JavaScript runtime built on Chrome’s V8 engine. It lets you run JavaScript outside the browser — on servers, CLIs, and embedded systems.

Why Node.js?

Use case Example
REST APIs User authentication, CRUD endpoints
Real-time apps Chat, live dashboards (WebSockets)
CLI tools Build scripts, code generators
Microservices Small, independent backend services
Serverless AWS Lambda, Vercel functions

How Node.js Works

  1. Single-threaded event loop — one main thread handles JavaScript execution
  2. Non-blocking I/O — file, network, and database operations run asynchronously
  3. libuv — C library that handles the event loop and thread pool for I/O
  Client Request → Event Loop → Callback/Promise → Response
                      ↓
              Thread Pool (I/O)
  

Node.js vs Browser JavaScript

Feature Browser Node.js
DOM Yes No
window Yes No (use global)
File system No Yes (fs module)
HTTP server No Yes (http module)
Modules ES modules CommonJS + ES modules

When to Use Node.js

Good fit:

  • I/O-heavy applications (APIs, proxies, streaming)
  • Real-time applications
  • Full-stack JavaScript teams (same language front and back)

Consider alternatives when:

  • CPU-intensive tasks dominate (video encoding, ML) — use workers or other languages
  • You need strong transactional database patterns — consider Java, C#, Go

The Node.js Ecosystem

  • npm — largest package registry (millions of packages)
  • Express — minimal web framework
  • NestJS — structured, TypeScript-first framework
  • Fastify — high-performance web framework
  • Socket.io — real-time communication

Hello, Node.js

Create hello.js:

  console.log('Hello from Node.js!');
console.log('Node version:', process.version);
console.log('Platform:', process.platform);
  

Run:

  node hello.js
  

In the next chapter, you’ll set up a full development environment and build your first HTTP server.