JavaScript (Node.js) Standard Library: Complete Overview
Node.js is a JavaScript runtime built on Google's V8 engine. One of its main advantages is its rich standard library, which provides ready-made modules for solving a wide variety of tasks: from working with the file system to creating HTTP servers. In this article, we will take a detailed look at the key modules of the Node.js standard library, show examples of their use, and explain when to use which module.
1. The fs Module (File System)
The fs module allows you to work with the file system: read, write, delete files and directories, as well as track changes. It provides both synchronous and asynchronous methods.
const fs = require('fs');
// Asynchronous file readingfs.readFile('example.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data);});
// Synchronous file writingfs.writeFileSync('output.txt', 'Hello, Node.js!');fs.readFile()— asynchronous file reading.fs.writeFileSync()— synchronous data writing to a file.fs.mkdir()— creating a directory.fs.unlink()— deleting a file.
2. The path Module
The path module provides utilities for working with file and directory paths. It automatically handles differences between operating systems (Windows, Linux, macOS).
const path = require('path');
const filePath = '/home/user/docs/file.txt';console.log(path.dirname(filePath)); // /home/user/docsconsole.log(path.basename(filePath)); // file.txtconsole.log(path.extname(filePath)); // .txtpath.join()— joins path segments.path.resolve()— resolves a relative path to an absolute path.path.normalize()— normalizes a path (removes extra..and.).
3. The http Module
The http module allows you to create HTTP servers and clients. It is the foundation for building web applications and APIs.
const http = require('http');
const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!');});
server.listen(3000, () => { console.log('Server running on port 3000');});http.createServer()— creates a server instance.http.request()— makes an HTTP request to an external resource.http.get()— a simplified version ofrequest()for GET requests.
4. The events Module
The events module implements the Observer pattern. It allows you to create custom events and subscribe to them. Most Node.js objects (e.g., streams, servers) inherit from EventEmitter.
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();myEmitter.on('event', () => { console.log('An event occurred!');});myEmitter.emit('event');emitter.on()— subscribes to an event.emitter.emit()— emits an event.emitter.once()— subscribes to a one-time event.
5. The stream Module
Streams are a fundamental concept in Node.js for working with data that is processed in chunks rather than all at once. This is especially useful for
Also in library
Jackson for Java: Complete Guide with Examples
Working with Markdown in Go: libraries blackfriday, goldmark and chroma
Creating CLI Tools in Node.js: Overview of Commander, Yargs, and Inquirer Frameworks
TypeScript Application Configuration: Libraries and Approaches