Skip to content

Chapter 6 of 8

Core Built-in Modules

Node.js ships with a rich set of built-in modules that cover most server-side needs without external dependencies. The fs module handles file system operations: fs.readFile and fs.writeFile read and write entire files, fs.appendFile appends content, fs.mkdir and fs.rmdir manage directories, fs.readdir lists directory contents, fs.stat returns file metadata, fs.unlink deletes files, fs.rename moves or renames files, and fs.watch observes file changes. For modern code, the fs/promises variant is recommended, returning Promises that work naturally with async/await. For large files, streaming with fs.createReadStream and fs.createWriteStream avoids loading everything into memory at once.

The path module provides utilities for working with file and directory paths in a cross-platform way. path.join concatenates segments with the correct separator, path.resolve converts a relative path into an absolute path, path.basename returns the file name, path.dirname returns the directory, and path.extname returns the file extension. Using these helpers avoids subtle bugs from manual string concatenation, especially when code needs to run on different operating systems. The built-in http module lets you create servers and make HTTP requests without external libraries, exposing the underlying functionality that Express is built on top of.

For more advanced scenarios, several modules enable concurrency and subprocess control. The child_process module spawns external commands: exec runs a shell command and buffers all output, which is best for short commands; execFile runs an executable directly without a shell; spawn streams output for long-running processes or large data; and fork creates a new Node.js process with an IPC channel for direct messaging. The cluster module leverages multiple CPU cores by forking worker processes that share the same server port, with the OS distributing connections in a round-robin fashion. For CPU-intensive JavaScript work, worker_threads provides true parallel execution on threads within a single process, communicating via postMessage and 'message' events, ideal for tasks like image processing or heavy data parsing that would otherwise block the event loop.

All chapters
  1. 1Foundations of Node.js and the Event Loop
  2. 2Module Systems
  3. 3Streams and Buffers
  4. 4Asynchronous Programming
  5. 5Building Web Applications with Express
  6. 6Core Built-in Modules
  7. 7Package Management and Project Configuration
  8. 8Error Handling, Process Management, and Events

Drill it

Reading is not remembering. These come from the Nodejs Runtime deck:

Q

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 engine. It allows running JavaScript outside the browser, using an event-driven, non-blocking I/O model tha...

Q

What is the Node.js event loop?

The event loop is the mechanism that allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It continuously checks the...

Q

What are the phases of the Node.js event loop?

Timers – executes setTimeout and setInterval callbacksPending callbacks – I/O callbacks deferred to next iterationPoll – retrieves new I/O eventsCheck – execute...

Q

What is the difference between setImmediate() and setTimeout()?

setImmediate() executes in the check phase of the event loop, after the poll phase completes. setTimeout(fn, 0) executes in the timers phase on the next iterati...